michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "MobileMessageCallback.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCxPusher.h" michael@0: #include "nsIDOMMozSmsMessage.h" michael@0: #include "nsIDOMMozMmsMessage.h" michael@0: #include "nsIDOMSmsSegmentInfo.h" michael@0: #include "nsIScriptGlobalObject.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "MmsMessage.h" michael@0: #include "jsapi.h" michael@0: #include "xpcpublic.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsTArrayHelpers.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: namespace mobilemessage { michael@0: michael@0: NS_IMPL_ADDREF(MobileMessageCallback) michael@0: NS_IMPL_RELEASE(MobileMessageCallback) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(MobileMessageCallback) michael@0: NS_INTERFACE_MAP_ENTRY(nsIMobileMessageCallback) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: MobileMessageCallback::MobileMessageCallback(DOMRequest* aDOMRequest) michael@0: : mDOMRequest(aDOMRequest) michael@0: { michael@0: } michael@0: michael@0: MobileMessageCallback::~MobileMessageCallback() michael@0: { michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: MobileMessageCallback::NotifySuccess(JS::Handle aResult, bool aAsync) michael@0: { michael@0: if (aAsync) { michael@0: nsCOMPtr rs = michael@0: do_GetService(DOMREQUEST_SERVICE_CONTRACTID); michael@0: NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); michael@0: michael@0: return rs->FireSuccessAsync(mDOMRequest, aResult); michael@0: } michael@0: michael@0: mDOMRequest->FireSuccess(aResult); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: MobileMessageCallback::NotifySuccess(nsISupports *aMessage, bool aAsync) michael@0: { michael@0: nsresult rv; michael@0: nsIScriptContext* scriptContext = mDOMRequest->GetContextForEventHandlers(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: NS_ENSURE_TRUE(scriptContext, NS_ERROR_FAILURE); michael@0: michael@0: AutoPushJSContext cx(scriptContext->GetNativeContext()); michael@0: NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE); michael@0: michael@0: JS::Rooted global(cx, scriptContext->GetWindowProxy()); michael@0: NS_ENSURE_TRUE(global, NS_ERROR_FAILURE); michael@0: michael@0: JSAutoCompartment ac(cx, global); michael@0: michael@0: JS::Rooted wrappedMessage(cx); michael@0: rv = nsContentUtils::WrapNative(cx, aMessage, &wrappedMessage); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NotifySuccess(wrappedMessage, aAsync); michael@0: } michael@0: michael@0: nsresult michael@0: MobileMessageCallback::NotifyError(int32_t aError, bool aAsync) michael@0: { michael@0: nsAutoString errorStr; michael@0: switch (aError) { michael@0: case nsIMobileMessageCallback::NO_SIGNAL_ERROR: michael@0: errorStr = NS_LITERAL_STRING("NoSignalError"); michael@0: break; michael@0: case nsIMobileMessageCallback::NOT_FOUND_ERROR: michael@0: errorStr = NS_LITERAL_STRING("NotFoundError"); michael@0: break; michael@0: case nsIMobileMessageCallback::UNKNOWN_ERROR: michael@0: errorStr = NS_LITERAL_STRING("UnknownError"); michael@0: break; michael@0: case nsIMobileMessageCallback::INTERNAL_ERROR: michael@0: errorStr = NS_LITERAL_STRING("InternalError"); michael@0: break; michael@0: case nsIMobileMessageCallback::NO_SIM_CARD_ERROR: michael@0: errorStr = NS_LITERAL_STRING("NoSimCardError"); michael@0: break; michael@0: case nsIMobileMessageCallback::RADIO_DISABLED_ERROR: michael@0: errorStr = NS_LITERAL_STRING("RadioDisabledError"); michael@0: break; michael@0: case nsIMobileMessageCallback::INVALID_ADDRESS_ERROR: michael@0: errorStr = NS_LITERAL_STRING("InvalidAddressError"); michael@0: break; michael@0: case nsIMobileMessageCallback::FDN_CHECK_ERROR: michael@0: errorStr = NS_LITERAL_STRING("FdnCheckError"); michael@0: break; michael@0: case nsIMobileMessageCallback::NON_ACTIVE_SIM_CARD_ERROR: michael@0: errorStr = NS_LITERAL_STRING("NonActiveSimCardError"); michael@0: break; michael@0: case nsIMobileMessageCallback::STORAGE_FULL_ERROR: michael@0: errorStr = NS_LITERAL_STRING("StorageFullError"); michael@0: break; michael@0: case nsIMobileMessageCallback::SIM_NOT_MATCHED_ERROR: michael@0: errorStr = NS_LITERAL_STRING("SimNotMatchedError"); michael@0: break; michael@0: default: // SUCCESS_NO_ERROR is handled above. michael@0: MOZ_CRASH("Should never get here!"); michael@0: } michael@0: michael@0: if (aAsync) { michael@0: nsCOMPtr rs = michael@0: do_GetService(DOMREQUEST_SERVICE_CONTRACTID); michael@0: NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); michael@0: michael@0: return rs->FireErrorAsync(mDOMRequest, errorStr); michael@0: } michael@0: michael@0: mDOMRequest->FireError(errorStr); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyMessageSent(nsISupports *aMessage) michael@0: { michael@0: return NotifySuccess(aMessage); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifySendMessageFailed(int32_t aError) michael@0: { michael@0: return NotifyError(aError); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyMessageGot(nsISupports *aMessage) michael@0: { michael@0: return NotifySuccess(aMessage); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyGetMessageFailed(int32_t aError) michael@0: { michael@0: return NotifyError(aError); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyMessageDeleted(bool *aDeleted, uint32_t aSize) michael@0: { michael@0: if (aSize == 1) { michael@0: AutoJSContext cx; michael@0: JS::Rooted val(cx, aDeleted[0] ? JSVAL_TRUE : JSVAL_FALSE); michael@0: return NotifySuccess(val); michael@0: } michael@0: michael@0: nsresult rv; michael@0: nsIScriptContext* sc = mDOMRequest->GetContextForEventHandlers(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: NS_ENSURE_TRUE(sc, NS_ERROR_FAILURE); michael@0: michael@0: AutoPushJSContext cx(sc->GetNativeContext()); michael@0: NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE); michael@0: michael@0: JS::Rooted deleteArrayObj(cx, michael@0: JS_NewArrayObject(cx, aSize)); michael@0: for (uint32_t i = 0; i < aSize; i++) { michael@0: JS_SetElement(cx, deleteArrayObj, i, aDeleted[i]); michael@0: } michael@0: michael@0: JS::Rooted deleteArrayVal(cx, JS::ObjectValue(*deleteArrayObj)); michael@0: return NotifySuccess(deleteArrayVal); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyDeleteMessageFailed(int32_t aError) michael@0: { michael@0: return NotifyError(aError); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyMessageMarkedRead(bool aRead) michael@0: { michael@0: AutoJSContext cx; michael@0: JS::Rooted val(cx, aRead ? JSVAL_TRUE : JSVAL_FALSE); michael@0: return NotifySuccess(val); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyMarkMessageReadFailed(int32_t aError) michael@0: { michael@0: return NotifyError(aError); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifySegmentInfoForTextGot(nsIDOMMozSmsSegmentInfo *aInfo) michael@0: { michael@0: return NotifySuccess(aInfo, true); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyGetSegmentInfoForTextFailed(int32_t aError) michael@0: { michael@0: return NotifyError(aError, true); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyGetSmscAddress(const nsAString& aSmscAddress) michael@0: { michael@0: AutoJSContext cx; michael@0: JSString* smsc = JS_NewUCStringCopyN(cx, michael@0: static_cast(aSmscAddress.BeginReading()), michael@0: aSmscAddress.Length()); michael@0: michael@0: if (!smsc) { michael@0: return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR); michael@0: } michael@0: michael@0: JS::Rooted val(cx, STRING_TO_JSVAL(smsc)); michael@0: return NotifySuccess(val); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MobileMessageCallback::NotifyGetSmscAddressFailed(int32_t aError) michael@0: { michael@0: return NotifyError(aError); michael@0: } michael@0: michael@0: } // namesapce mobilemessage michael@0: } // namespace dom michael@0: } // namespace mozilla