2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-03-09 07:22:10 +00: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/. */
|
|
|
|
|
|
|
|
#include "MobileMessageCallback.h"
|
2014-08-04 09:01:36 +00:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2013-03-09 07:22:10 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDOMMozSmsMessage.h"
|
|
|
|
#include "nsIDOMMozMmsMessage.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "MmsMessage.h"
|
2014-06-25 21:09:40 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-03-09 07:22:10 +00:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "xpcpublic.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-04-29 09:50:55 +00:00
|
|
|
#include "nsTArrayHelpers.h"
|
2014-04-28 05:20:02 +00:00
|
|
|
#include "DOMMobileMessageError.h"
|
2015-04-29 02:58:16 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-03-09 07:22:10 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace mobilemessage {
|
|
|
|
|
2014-04-28 05:20:02 +00: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-09 07:22:10 +00: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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:58:16 +00:00
|
|
|
MobileMessageCallback::MobileMessageCallback(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:22:10 +00:00
|
|
|
MobileMessageCallback::~MobileMessageCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-08 04:06:06 +00:00
|
|
|
|
|
|
|
nsresult
|
2013-08-09 13:25:53 +00:00
|
|
|
MobileMessageCallback::NotifySuccess(JS::Handle<JS::Value> aResult, bool aAsync)
|
2013-03-08 04:06:06 +00:00
|
|
|
{
|
2015-05-13 03:42:42 +00:00
|
|
|
if (NS_WARN_IF(!mDOMRequest->GetOwner())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-08-09 13:25:53 +00: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-09 02:21:31 +00:00
|
|
|
mDOMRequest->FireSuccess(aResult);
|
|
|
|
return NS_OK;
|
2013-03-08 04:06:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 03:46:16 +00:00
|
|
|
nsresult
|
2013-08-09 13:25:53 +00:00
|
|
|
MobileMessageCallback::NotifySuccess(nsISupports *aMessage, bool aAsync)
|
2013-03-09 07:22:10 +00:00
|
|
|
{
|
2014-06-25 21:09:40 +00:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-04-09 02:21:31 +00:00
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> wrappedMessage(cx);
|
2014-06-25 21:09:40 +00:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(cx, aMessage, &wrappedMessage);
|
2013-03-09 07:22:10 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-08-09 13:25:53 +00:00
|
|
|
return NotifySuccess(wrappedMessage, aAsync);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 03:46:16 +00:00
|
|
|
nsresult
|
2014-04-28 05:20:02 +00:00
|
|
|
MobileMessageCallback::NotifyError(int32_t aError, DOMError *aDetailedError, bool aAsync)
|
2013-03-09 07:22:10 +00:00
|
|
|
{
|
2015-05-13 03:42:42 +00:00
|
|
|
if (NS_WARN_IF(!mDOMRequest->GetOwner())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-08-09 13:25:53 +00:00
|
|
|
if (aAsync) {
|
2014-04-28 05:20:02 +00:00
|
|
|
NS_ASSERTION(!aDetailedError,
|
|
|
|
"No Support to FireDetailedErrorAsync() in nsIDOMRequestService!");
|
|
|
|
|
2013-08-09 13:25:53 +00:00
|
|
|
nsCOMPtr<nsIDOMRequestService> rs =
|
|
|
|
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
|
|
|
|
|
2014-04-28 05:20:02 +00:00
|
|
|
return rs->FireErrorAsync(mDOMRequest,
|
|
|
|
ConvertErrorCodeToErrorString(aError));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDetailedError) {
|
|
|
|
mDOMRequest->FireDetailedError(aDetailedError);
|
|
|
|
} else {
|
|
|
|
mDOMRequest->FireError(ConvertErrorCodeToErrorString(aError));
|
2013-08-09 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-09 07:22:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-08 03:46:16 +00:00
|
|
|
MobileMessageCallback::NotifyMessageSent(nsISupports *aMessage)
|
2013-03-09 07:22:10 +00:00
|
|
|
{
|
2013-03-08 03:46:16 +00:00
|
|
|
return NotifySuccess(aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-04-28 04:33:10 +00:00
|
|
|
MobileMessageCallback::NotifySendMessageFailed(int32_t aError, nsISupports *aMessage)
|
2013-03-08 03:46:16 +00:00
|
|
|
{
|
2015-05-13 03:42:42 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = mDOMRequest->GetOwner();
|
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-28 05:20:02 +00:00
|
|
|
nsRefPtr<DOMMobileMessageError> domMobileMessageError;
|
|
|
|
if (aMessage) {
|
|
|
|
nsAutoString errorStr = ConvertErrorCodeToErrorString(aError);
|
|
|
|
nsCOMPtr<nsIDOMMozSmsMessage> smsMsg = do_QueryInterface(aMessage);
|
|
|
|
if (smsMsg) {
|
|
|
|
domMobileMessageError =
|
2015-05-13 03:42:42 +00:00
|
|
|
new DOMMobileMessageError(window, errorStr, smsMsg);
|
2014-04-28 05:20:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIDOMMozMmsMessage> mmsMsg = do_QueryInterface(aMessage);
|
|
|
|
domMobileMessageError =
|
2015-05-13 03:42:42 +00:00
|
|
|
new DOMMobileMessageError(window, errorStr, mmsMsg);
|
2014-04-28 05:20:02 +00:00
|
|
|
}
|
|
|
|
NS_ASSERTION(domMobileMessageError, "Invalid DOMMobileMessageError!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NotifyError(aError, domMobileMessageError);
|
2013-03-08 03:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyMessageGot(nsISupports *aMessage)
|
|
|
|
{
|
|
|
|
return NotifySuccess(aMessage);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetMessageFailed(int32_t aError)
|
|
|
|
{
|
2013-03-08 03:46:16 +00:00
|
|
|
return NotifyError(aError);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-29 09:50:55 +00:00
|
|
|
MobileMessageCallback::NotifyMessageDeleted(bool *aDeleted, uint32_t aSize)
|
2013-03-09 07:22:10 +00:00
|
|
|
{
|
2013-04-29 09:50:55 +00:00
|
|
|
if (aSize == 1) {
|
2013-05-18 01:48:25 +00:00
|
|
|
AutoJSContext cx;
|
2015-06-30 01:36:44 +00:00
|
|
|
JS::Rooted<JS::Value> val(cx, JS::BooleanValue(*aDeleted));
|
2013-05-18 01:48:25 +00:00
|
|
|
return NotifySuccess(val);
|
2013-04-29 09:50:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 23:35:51 +00:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mDOMRequest->GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-04-29 09:50:55 +00:00
|
|
|
|
2014-06-25 23:35:51 +00:00
|
|
|
JS::Rooted<JSObject*> deleteArrayObj(cx, JS_NewArrayObject(cx, aSize));
|
2013-04-29 09:50:55 +00:00
|
|
|
for (uint32_t i = 0; i < aSize; i++) {
|
2015-03-02 13:31:15 +00:00
|
|
|
JS_DefineElement(cx, deleteArrayObj, i, aDeleted[i], JSPROP_ENUMERATE);
|
2013-04-29 09:50:55 +00:00
|
|
|
}
|
|
|
|
|
2013-05-18 01:48:25 +00:00
|
|
|
JS::Rooted<JS::Value> deleteArrayVal(cx, JS::ObjectValue(*deleteArrayObj));
|
|
|
|
return NotifySuccess(deleteArrayVal);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyDeleteMessageFailed(int32_t aError)
|
|
|
|
{
|
2013-03-08 04:06:06 +00:00
|
|
|
return NotifyError(aError);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyMessageMarkedRead(bool aRead)
|
|
|
|
{
|
2013-05-18 01:48:25 +00:00
|
|
|
AutoJSContext cx;
|
2015-06-30 01:36:44 +00:00
|
|
|
JS::Rooted<JS::Value> val(cx, JS::BooleanValue(aRead));
|
2013-05-18 01:48:25 +00:00
|
|
|
return NotifySuccess(val);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyMarkMessageReadFailed(int32_t aError)
|
|
|
|
{
|
2013-03-11 04:14:15 +00:00
|
|
|
return NotifyError(aError);
|
2013-03-09 07:22:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 13:25:53 +00:00
|
|
|
NS_IMETHODIMP
|
2014-08-04 09:01:36 +00:00
|
|
|
MobileMessageCallback::NotifySegmentInfoForTextGot(int32_t aSegments,
|
|
|
|
int32_t aCharsPerSegment,
|
|
|
|
int32_t aCharsAvailableInLastSegment)
|
2013-08-09 13:25:53 +00:00
|
|
|
{
|
2014-08-04 09:01:36 +00: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 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetSegmentInfoForTextFailed(int32_t aError)
|
|
|
|
{
|
2014-04-28 05:20:02 +00:00
|
|
|
return NotifyError(aError, nullptr, true);
|
2013-08-09 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 02:12:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetSmscAddress(const nsAString& aSmscAddress)
|
|
|
|
{
|
2014-07-03 03:31:52 +00: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-16 02:12:38 +00:00
|
|
|
aSmscAddress.Length());
|
|
|
|
|
|
|
|
if (!smsc) {
|
|
|
|
return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-06-30 18:20:56 +00:00
|
|
|
JS::Rooted<JS::Value> val(cx, JS::StringValue(smsc));
|
2013-09-16 02:12:38 +00:00
|
|
|
return NotifySuccess(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifyGetSmscAddressFailed(int32_t aError)
|
|
|
|
{
|
|
|
|
return NotifyError(aError);
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:58:16 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifySetSmscAddress()
|
|
|
|
{
|
|
|
|
mPromise->MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileMessageCallback::NotifySetSmscAddressFailed(int32_t aError)
|
|
|
|
{
|
|
|
|
const nsAString& errorStr = ConvertErrorCodeToErrorString(aError);
|
|
|
|
mPromise->MaybeRejectBrokenly(errorStr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:22:10 +00:00
|
|
|
} // namesapce mobilemessage
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|