gecko-dev/dom/mobilemessage/MobileMessageCallback.cpp
Birunthan Mohanathas 858dfa00a5 Bug 1058101 - Flatten dom/mobilemessage/src/ into parent directory. r=mccr8
--HG--
rename : dom/mobilemessage/src/Constants.cpp => dom/mobilemessage/Constants.cpp
rename : dom/mobilemessage/src/Constants.h => dom/mobilemessage/Constants.h
rename : dom/mobilemessage/src/DOMMobileMessageError.cpp => dom/mobilemessage/DOMMobileMessageError.cpp
rename : dom/mobilemessage/src/DOMMobileMessageError.h => dom/mobilemessage/DOMMobileMessageError.h
rename : dom/mobilemessage/src/DeletedMessageInfo.cpp => dom/mobilemessage/DeletedMessageInfo.cpp
rename : dom/mobilemessage/src/DeletedMessageInfo.h => dom/mobilemessage/DeletedMessageInfo.h
rename : dom/mobilemessage/src/MmsMessage.cpp => dom/mobilemessage/MmsMessage.cpp
rename : dom/mobilemessage/src/MmsMessage.h => dom/mobilemessage/MmsMessage.h
rename : dom/mobilemessage/src/MobileMessageCallback.cpp => dom/mobilemessage/MobileMessageCallback.cpp
rename : dom/mobilemessage/src/MobileMessageCallback.h => dom/mobilemessage/MobileMessageCallback.h
rename : dom/mobilemessage/src/MobileMessageCursorCallback.cpp => dom/mobilemessage/MobileMessageCursorCallback.cpp
rename : dom/mobilemessage/src/MobileMessageCursorCallback.h => dom/mobilemessage/MobileMessageCursorCallback.h
rename : dom/mobilemessage/src/MobileMessageManager.cpp => dom/mobilemessage/MobileMessageManager.cpp
rename : dom/mobilemessage/src/MobileMessageManager.h => dom/mobilemessage/MobileMessageManager.h
rename : dom/mobilemessage/src/MobileMessageService.cpp => dom/mobilemessage/MobileMessageService.cpp
rename : dom/mobilemessage/src/MobileMessageService.h => dom/mobilemessage/MobileMessageService.h
rename : dom/mobilemessage/src/MobileMessageThread.cpp => dom/mobilemessage/MobileMessageThread.cpp
rename : dom/mobilemessage/src/MobileMessageThread.h => dom/mobilemessage/MobileMessageThread.h
rename : dom/mobilemessage/src/SmsMessage.cpp => dom/mobilemessage/SmsMessage.cpp
rename : dom/mobilemessage/src/SmsMessage.h => dom/mobilemessage/SmsMessage.h
rename : dom/mobilemessage/src/SmsServicesFactory.cpp => dom/mobilemessage/SmsServicesFactory.cpp
rename : dom/mobilemessage/src/SmsServicesFactory.h => dom/mobilemessage/SmsServicesFactory.h
rename : dom/mobilemessage/src/Types.h => dom/mobilemessage/Types.h
rename : dom/mobilemessage/src/android/MobileMessageDatabaseService.cpp => dom/mobilemessage/android/MobileMessageDatabaseService.cpp
rename : dom/mobilemessage/src/android/MobileMessageDatabaseService.h => dom/mobilemessage/android/MobileMessageDatabaseService.h
rename : dom/mobilemessage/src/android/SmsService.cpp => dom/mobilemessage/android/SmsService.cpp
rename : dom/mobilemessage/src/android/SmsService.h => dom/mobilemessage/android/SmsService.h
rename : dom/mobilemessage/src/gonk/MmsPduHelper.jsm => dom/mobilemessage/gonk/MmsPduHelper.jsm
rename : dom/mobilemessage/src/gonk/MmsService.js => dom/mobilemessage/gonk/MmsService.js
rename : dom/mobilemessage/src/gonk/MmsService.manifest => dom/mobilemessage/gonk/MmsService.manifest
rename : dom/mobilemessage/src/gonk/MobileMessageDB.jsm => dom/mobilemessage/gonk/MobileMessageDB.jsm
rename : dom/mobilemessage/src/gonk/MobileMessageDatabaseService.js => dom/mobilemessage/gonk/MobileMessageDatabaseService.js
rename : dom/mobilemessage/src/gonk/MobileMessageDatabaseService.manifest => dom/mobilemessage/gonk/MobileMessageDatabaseService.manifest
rename : dom/mobilemessage/src/gonk/SmsService.cpp => dom/mobilemessage/gonk/SmsService.cpp
rename : dom/mobilemessage/src/gonk/SmsService.h => dom/mobilemessage/gonk/SmsService.h
rename : dom/mobilemessage/src/gonk/WspPduHelper.jsm => dom/mobilemessage/gonk/WspPduHelper.jsm
rename : dom/mobilemessage/src/gonk/mms_consts.js => dom/mobilemessage/gonk/mms_consts.js
rename : dom/mobilemessage/src/gonk/wap_consts.js => dom/mobilemessage/gonk/wap_consts.js
rename : dom/mobilemessage/src/ipc/PMobileMessageCursor.ipdl => dom/mobilemessage/ipc/PMobileMessageCursor.ipdl
rename : dom/mobilemessage/src/ipc/PSms.ipdl => dom/mobilemessage/ipc/PSms.ipdl
rename : dom/mobilemessage/src/ipc/PSmsRequest.ipdl => dom/mobilemessage/ipc/PSmsRequest.ipdl
rename : dom/mobilemessage/src/ipc/SmsChild.cpp => dom/mobilemessage/ipc/SmsChild.cpp
rename : dom/mobilemessage/src/ipc/SmsChild.h => dom/mobilemessage/ipc/SmsChild.h
rename : dom/mobilemessage/src/ipc/SmsIPCService.cpp => dom/mobilemessage/ipc/SmsIPCService.cpp
rename : dom/mobilemessage/src/ipc/SmsIPCService.h => dom/mobilemessage/ipc/SmsIPCService.h
rename : dom/mobilemessage/src/ipc/SmsParent.cpp => dom/mobilemessage/ipc/SmsParent.cpp
rename : dom/mobilemessage/src/ipc/SmsParent.h => dom/mobilemessage/ipc/SmsParent.h
rename : dom/mobilemessage/src/ipc/SmsTypes.ipdlh => dom/mobilemessage/ipc/SmsTypes.ipdlh
2014-08-27 10:13:36 -07:00

286 lines
8.0 KiB
C++

/* -*- 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 "MobileMessageCallback.h"
#include "mozilla/dom/ToJSValue.h"
#include "nsContentUtils.h"
#include "nsIDOMMozSmsMessage.h"
#include "nsIDOMMozMmsMessage.h"
#include "nsIScriptGlobalObject.h"
#include "nsPIDOMWindow.h"
#include "MmsMessage.h"
#include "mozilla/dom/ScriptSettings.h"
#include "jsapi.h"
#include "xpcpublic.h"
#include "nsServiceManagerUtils.h"
#include "nsTArrayHelpers.h"
#include "DOMMobileMessageError.h"
namespace mozilla {
namespace dom {
namespace mobilemessage {
static nsAutoString
ConvertErrorCodeToErrorString(int32_t aError)
{
nsAutoString errorStr;
switch (aError) {
case nsIMobileMessageCallback::NO_SIGNAL_ERROR:
errorStr = NS_LITERAL_STRING("NoSignalError");
break;
case nsIMobileMessageCallback::NOT_FOUND_ERROR:
errorStr = NS_LITERAL_STRING("NotFoundError");
break;
case nsIMobileMessageCallback::UNKNOWN_ERROR:
errorStr = NS_LITERAL_STRING("UnknownError");
break;
case nsIMobileMessageCallback::INTERNAL_ERROR:
errorStr = NS_LITERAL_STRING("InternalError");
break;
case nsIMobileMessageCallback::NO_SIM_CARD_ERROR:
errorStr = NS_LITERAL_STRING("NoSimCardError");
break;
case nsIMobileMessageCallback::RADIO_DISABLED_ERROR:
errorStr = NS_LITERAL_STRING("RadioDisabledError");
break;
case nsIMobileMessageCallback::INVALID_ADDRESS_ERROR:
errorStr = NS_LITERAL_STRING("InvalidAddressError");
break;
case nsIMobileMessageCallback::FDN_CHECK_ERROR:
errorStr = NS_LITERAL_STRING("FdnCheckError");
break;
case nsIMobileMessageCallback::NON_ACTIVE_SIM_CARD_ERROR:
errorStr = NS_LITERAL_STRING("NonActiveSimCardError");
break;
case nsIMobileMessageCallback::STORAGE_FULL_ERROR:
errorStr = NS_LITERAL_STRING("StorageFullError");
break;
case nsIMobileMessageCallback::SIM_NOT_MATCHED_ERROR:
errorStr = NS_LITERAL_STRING("SimNotMatchedError");
break;
default: // SUCCESS_NO_ERROR is handled above.
MOZ_CRASH("Should never get here!");
}
return errorStr;
}
NS_IMPL_ADDREF(MobileMessageCallback)
NS_IMPL_RELEASE(MobileMessageCallback)
NS_INTERFACE_MAP_BEGIN(MobileMessageCallback)
NS_INTERFACE_MAP_ENTRY(nsIMobileMessageCallback)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
MobileMessageCallback::MobileMessageCallback(DOMRequest* aDOMRequest)
: mDOMRequest(aDOMRequest)
{
}
MobileMessageCallback::~MobileMessageCallback()
{
}
nsresult
MobileMessageCallback::NotifySuccess(JS::Handle<JS::Value> aResult, bool aAsync)
{
if (aAsync) {
nsCOMPtr<nsIDOMRequestService> rs =
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
return rs->FireSuccessAsync(mDOMRequest, aResult);
}
mDOMRequest->FireSuccess(aResult);
return NS_OK;
}
nsresult
MobileMessageCallback::NotifySuccess(nsISupports *aMessage, bool aAsync)
{
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
return NS_ERROR_FAILURE;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JS::Value> wrappedMessage(cx);
nsresult rv = nsContentUtils::WrapNative(cx, aMessage, &wrappedMessage);
NS_ENSURE_SUCCESS(rv, rv);
return NotifySuccess(wrappedMessage, aAsync);
}
nsresult
MobileMessageCallback::NotifyError(int32_t aError, DOMError *aDetailedError, bool aAsync)
{
if (aAsync) {
NS_ASSERTION(!aDetailedError,
"No Support to FireDetailedErrorAsync() in nsIDOMRequestService!");
nsCOMPtr<nsIDOMRequestService> rs =
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
return rs->FireErrorAsync(mDOMRequest,
ConvertErrorCodeToErrorString(aError));
}
if (aDetailedError) {
mDOMRequest->FireDetailedError(aDetailedError);
} else {
mDOMRequest->FireError(ConvertErrorCodeToErrorString(aError));
}
return NS_OK;
}
NS_IMETHODIMP
MobileMessageCallback::NotifyMessageSent(nsISupports *aMessage)
{
return NotifySuccess(aMessage);
}
NS_IMETHODIMP
MobileMessageCallback::NotifySendMessageFailed(int32_t aError, nsISupports *aMessage)
{
nsRefPtr<DOMMobileMessageError> domMobileMessageError;
if (aMessage) {
nsAutoString errorStr = ConvertErrorCodeToErrorString(aError);
nsCOMPtr<nsIDOMMozSmsMessage> smsMsg = do_QueryInterface(aMessage);
if (smsMsg) {
domMobileMessageError =
new DOMMobileMessageError(mDOMRequest->GetOwner(), errorStr, smsMsg);
}
else {
nsCOMPtr<nsIDOMMozMmsMessage> mmsMsg = do_QueryInterface(aMessage);
domMobileMessageError =
new DOMMobileMessageError(mDOMRequest->GetOwner(), errorStr, mmsMsg);
}
NS_ASSERTION(domMobileMessageError, "Invalid DOMMobileMessageError!");
}
return NotifyError(aError, domMobileMessageError);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyMessageGot(nsISupports *aMessage)
{
return NotifySuccess(aMessage);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyGetMessageFailed(int32_t aError)
{
return NotifyError(aError);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyMessageDeleted(bool *aDeleted, uint32_t aSize)
{
if (aSize == 1) {
AutoJSContext cx;
JS::Rooted<JS::Value> val(cx, aDeleted[0] ? JSVAL_TRUE : JSVAL_FALSE);
return NotifySuccess(val);
}
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
return NS_ERROR_FAILURE;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JSObject*> deleteArrayObj(cx, JS_NewArrayObject(cx, aSize));
for (uint32_t i = 0; i < aSize; i++) {
JS_SetElement(cx, deleteArrayObj, i, aDeleted[i]);
}
JS::Rooted<JS::Value> deleteArrayVal(cx, JS::ObjectValue(*deleteArrayObj));
return NotifySuccess(deleteArrayVal);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyDeleteMessageFailed(int32_t aError)
{
return NotifyError(aError);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyMessageMarkedRead(bool aRead)
{
AutoJSContext cx;
JS::Rooted<JS::Value> val(cx, aRead ? JSVAL_TRUE : JSVAL_FALSE);
return NotifySuccess(val);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyMarkMessageReadFailed(int32_t aError)
{
return NotifyError(aError);
}
NS_IMETHODIMP
MobileMessageCallback::NotifySegmentInfoForTextGot(int32_t aSegments,
int32_t aCharsPerSegment,
int32_t aCharsAvailableInLastSegment)
{
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR);
}
SmsSegmentInfo info;
info.mSegments = aSegments;
info.mCharsPerSegment = aCharsPerSegment;
info.mCharsAvailableInLastSegment = aCharsAvailableInLastSegment;
JSContext* cx = jsapi.cx();
JS::Rooted<JS::Value> val(cx);
if (!ToJSValue(cx, info, &val)) {
JS_ClearPendingException(cx);
return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR);
}
return NotifySuccess(val, true);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyGetSegmentInfoForTextFailed(int32_t aError)
{
return NotifyError(aError, nullptr, true);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyGetSmscAddress(const nsAString& aSmscAddress)
{
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR);
}
JSContext* cx = jsapi.cx();
JSString* smsc = JS_NewUCStringCopyN(cx, aSmscAddress.BeginReading(),
aSmscAddress.Length());
if (!smsc) {
return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR);
}
JS::Rooted<JS::Value> val(cx, STRING_TO_JSVAL(smsc));
return NotifySuccess(val);
}
NS_IMETHODIMP
MobileMessageCallback::NotifyGetSmscAddressFailed(int32_t aError)
{
return NotifyError(aError);
}
} // namesapce mobilemessage
} // namespace dom
} // namespace mozilla