2013-03-08 23:22:10 -08:00
|
|
|
/* -*- 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"
|
2014-08-04 02:01:36 -07:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2013-03-08 23:22:10 -08:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDOMMozSmsMessage.h"
|
|
|
|
#include "nsIDOMMozMmsMessage.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "MmsMessage.h"
|
2014-06-25 14:09:40 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-03-08 23:22:10 -08:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "xpcpublic.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-04-29 02:50:55 -07:00
|
|
|
#include "nsTArrayHelpers.h"
|
2014-04-27 22:20:02 -07:00
|
|
|
#include "DOMMobileMessageError.h"
|
2013-03-08 23:22:10 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace mobilemessage {
|
|
|
|
|
2014-04-27 22:20:02 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-08 23:22:10 -08:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-07 20:06:06 -08:00
|
|
|
|
|
|
|
nsresult
|
2013-08-09 06:25:53 -07:00
|
|
|
MobileMessageCallback::NotifySuccess(JS::Handle<JS::Value> aResult, bool aAsync)
|
2013-03-07 20:06:06 -08:00
|
|
|
{
|
2013-08-09 06:25:53 -07:00
|
|
|
if (aAsync) {
|
|
|
|
nsCOMPtr<nsIDOMRequestService> rs =
|
|
|
|
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return rs->FireSuccessAsync(mDOMRequest, aResult);
|
|
|
|
}
|
|
|
|
|
2013-04-08 19:21:31 -07:00
|
|
|
mDOMRequest->FireSuccess(aResult);
|
|
|
|
return NS_OK;
|
2013-03-07 20:06:06 -08:00
|
|
|
}
|
|
|
|
|
2013-03-07 19:46:16 -08:00
|
|
|
nsresult
|
2013-08-09 06:25:53 -07:00
|
|
|
MobileMessageCallback::NotifySuccess(nsISupports *aMessage, bool aAsync)
|
2013-03-08 23:22:10 -08:00
|
|
|
{
|
2014-06-25 14:09:40 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-04-08 19:21:31 -07:00
|
|
|
|
2013-05-02 02:12:47 -07:00
|
|
|
JS::Rooted<JS::Value> wrappedMessage(cx);
|
2014-06-25 14:09:40 -07:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(cx, aMessage, &wrappedMessage);
|
2013-03-08 23:22:10 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-08-09 06:25:53 -07:00
|
|
|
return NotifySuccess(wrappedMessage, aAsync);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
2013-03-07 19:46:16 -08:00
|
|
|
nsresult
|
2014-04-27 22:20:02 -07:00
|
|
|
MobileMessageCallback::NotifyError(int32_t aError, DOMError *aDetailedError, bool aAsync)
|
2013-03-08 23:22:10 -08:00
|
|
|
{
|
2013-08-09 06:25:53 -07:00
|
|
|
if (aAsync) {
|
2014-04-27 22:20:02 -07:00
|
|
|
NS_ASSERTION(!aDetailedError,
|
|
|
|
"No Support to FireDetailedErrorAsync() in nsIDOMRequestService!");
|
|
|
|
|
2013-08-09 06:25:53 -07:00
|
|
|
nsCOMPtr<nsIDOMRequestService> rs =
|
|
|
|
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
|
|
|
|
|
2014-04-27 22:20:02 -07:00
|
|
|
return rs->FireErrorAsync(mDOMRequest,
|
|
|
|
ConvertErrorCodeToErrorString(aError));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDetailedError) {
|
|
|
|
mDOMRequest->FireDetailedError(aDetailedError);
|
|
|
|
} else {
|
|
|
|
mDOMRequest->FireError(ConvertErrorCodeToErrorString(aError));
|
2013-08-09 06:25:53 -07:00
|
|
|
}
|
|
|
|
|
2013-03-08 23:22:10 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-07 19:46:16 -08:00
|
|
|
MobileMessageCallback::NotifyMessageSent(nsISupports *aMessage)
|
2013-03-08 23:22:10 -08:00
|
|
|
{
|
2013-03-07 19:46:16 -08:00
|
|
|
return NotifySuccess(aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-04-27 21:33:10 -07:00
|
|
|
MobileMessageCallback::NotifySendMessageFailed(int32_t aError, nsISupports *aMessage)
|
2013-03-07 19:46:16 -08:00
|
|
|
{
|
2014-04-27 22:20:02 -07:00
|
|
|
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);
|
2013-03-07 19:46:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyMessageGot(nsISupports *aMessage)
|
|
|
|
{
|
|
|
|
return NotifySuccess(aMessage);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetMessageFailed(int32_t aError)
|
|
|
|
{
|
2013-03-07 19:46:16 -08:00
|
|
|
return NotifyError(aError);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-29 02:50:55 -07:00
|
|
|
MobileMessageCallback::NotifyMessageDeleted(bool *aDeleted, uint32_t aSize)
|
2013-03-08 23:22:10 -08:00
|
|
|
{
|
2013-04-29 02:50:55 -07:00
|
|
|
if (aSize == 1) {
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> val(cx, aDeleted[0] ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
return NotifySuccess(val);
|
2013-04-29 02:50:55 -07:00
|
|
|
}
|
|
|
|
|
2014-06-25 16:35:51 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-04-29 02:50:55 -07:00
|
|
|
|
2014-06-25 16:35:51 -07:00
|
|
|
JS::Rooted<JSObject*> deleteArrayObj(cx, JS_NewArrayObject(cx, aSize));
|
2013-04-29 02:50:55 -07:00
|
|
|
for (uint32_t i = 0; i < aSize; i++) {
|
2014-01-25 01:31:17 -08:00
|
|
|
JS_SetElement(cx, deleteArrayObj, i, aDeleted[i]);
|
2013-04-29 02:50:55 -07:00
|
|
|
}
|
|
|
|
|
2013-05-17 18:48:25 -07:00
|
|
|
JS::Rooted<JS::Value> deleteArrayVal(cx, JS::ObjectValue(*deleteArrayObj));
|
|
|
|
return NotifySuccess(deleteArrayVal);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyDeleteMessageFailed(int32_t aError)
|
|
|
|
{
|
2013-03-07 20:06:06 -08:00
|
|
|
return NotifyError(aError);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyMessageMarkedRead(bool aRead)
|
|
|
|
{
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> val(cx, aRead ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
return NotifySuccess(val);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyMarkMessageReadFailed(int32_t aError)
|
|
|
|
{
|
2013-03-10 21:14:15 -07:00
|
|
|
return NotifyError(aError);
|
2013-03-08 23:22:10 -08:00
|
|
|
}
|
|
|
|
|
2013-08-09 06:25:53 -07:00
|
|
|
NS_IMETHODIMP
|
2014-08-04 02:01:36 -07:00
|
|
|
MobileMessageCallback::NotifySegmentInfoForTextGot(int32_t aSegments,
|
|
|
|
int32_t aCharsPerSegment,
|
|
|
|
int32_t aCharsAvailableInLastSegment)
|
2013-08-09 06:25:53 -07:00
|
|
|
{
|
2014-08-04 02:01:36 -07:00
|
|
|
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);
|
2013-08-09 06:25:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetSegmentInfoForTextFailed(int32_t aError)
|
|
|
|
{
|
2014-04-27 22:20:02 -07:00
|
|
|
return NotifyError(aError, nullptr, true);
|
2013-08-09 06:25:53 -07:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:12:38 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetSmscAddress(const nsAString& aSmscAddress)
|
|
|
|
{
|
2014-07-02 20:31:52 -07:00
|
|
|
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(),
|
2013-09-15 19:12:38 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-08 23:22:10 -08:00
|
|
|
} // namesapce mobilemessage
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|