michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=99: */ 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: /* Data conversion between native and JavaScript types. */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "xpcprivate.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsWrapperCache.h" michael@0: #include "nsJSUtils.h" michael@0: #include "WrapperFactory.h" michael@0: michael@0: #include "nsWrapperCacheInlines.h" michael@0: michael@0: #include "jsapi.h" michael@0: #include "jsfriendapi.h" michael@0: #include "jsprf.h" michael@0: #include "JavaScriptParent.h" michael@0: michael@0: #include "mozilla/dom/BindingUtils.h" michael@0: #include "mozilla/dom/DOMException.h" michael@0: #include "mozilla/dom/PrimitiveConversions.h" michael@0: michael@0: using namespace xpc; michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: using namespace JS; michael@0: michael@0: //#define STRICT_CHECK_OF_UNICODE michael@0: #ifdef STRICT_CHECK_OF_UNICODE michael@0: #define ILLEGAL_RANGE(c) (0!=((c) & 0xFF80)) michael@0: #else // STRICT_CHECK_OF_UNICODE michael@0: #define ILLEGAL_RANGE(c) (0!=((c) & 0xFF00)) michael@0: #endif // STRICT_CHECK_OF_UNICODE michael@0: michael@0: #define ILLEGAL_CHAR_RANGE(c) (0!=((c) & 0x80)) michael@0: michael@0: /***********************************************************/ michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::IsMethodReflectable(const XPTMethodDescriptor& info) michael@0: { michael@0: if (XPT_MD_IS_NOTXPCOM(info.flags) || XPT_MD_IS_HIDDEN(info.flags)) michael@0: return false; michael@0: michael@0: for (int i = info.num_args-1; i >= 0; i--) { michael@0: const nsXPTParamInfo& param = info.params[i]; michael@0: const nsXPTType& type = param.GetType(); michael@0: michael@0: // Reflected methods can't use native types. All native types end up michael@0: // getting tagged as void*, so this check is easy. michael@0: if (type.TagPart() == nsXPTType::T_VOID) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static JSObject* michael@0: UnwrapNativeCPOW(nsISupports* wrapper) michael@0: { michael@0: nsCOMPtr underware = do_QueryInterface(wrapper); michael@0: if (underware) { michael@0: JSObject* mainObj = underware->GetJSObject(); michael@0: if (mainObj && mozilla::jsipc::JavaScriptParent::IsCPOW(mainObj)) michael@0: return mainObj; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::GetISupportsFromJSObject(JSObject* obj, nsISupports** iface) michael@0: { michael@0: const JSClass* jsclass = js::GetObjectJSClass(obj); michael@0: MOZ_ASSERT(jsclass, "obj has no class"); michael@0: if (jsclass && michael@0: (jsclass->flags & JSCLASS_HAS_PRIVATE) && michael@0: (jsclass->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS)) { michael@0: *iface = (nsISupports*) xpc_GetJSPrivate(obj); michael@0: return true; michael@0: } michael@0: *iface = UnwrapDOMObjectToISupports(obj); michael@0: return !!*iface; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::NativeData2JS(MutableHandleValue d, const void* s, michael@0: const nsXPTType& type, const nsID* iid, nsresult* pErr) michael@0: { michael@0: NS_PRECONDITION(s, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE; michael@0: michael@0: switch (type.TagPart()) { michael@0: case nsXPTType::T_I8 : michael@0: d.setInt32(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_I16 : michael@0: d.setInt32(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_I32 : michael@0: d.setInt32(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_I64 : michael@0: d.setNumber(static_cast(*static_cast(s))); michael@0: return true; michael@0: case nsXPTType::T_U8 : michael@0: d.setInt32(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_U16 : michael@0: d.setInt32(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_U32 : michael@0: d.setNumber(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_U64 : michael@0: d.setNumber(static_cast(*static_cast(s))); michael@0: return true; michael@0: case nsXPTType::T_FLOAT : michael@0: d.setNumber(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_DOUBLE: michael@0: d.setNumber(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_BOOL : michael@0: d.setBoolean(*static_cast(s)); michael@0: return true; michael@0: case nsXPTType::T_CHAR : michael@0: { michael@0: char p = *static_cast(s); michael@0: michael@0: #ifdef STRICT_CHECK_OF_UNICODE michael@0: MOZ_ASSERT(! ILLEGAL_CHAR_RANGE(p) , "passing non ASCII data"); michael@0: #endif // STRICT_CHECK_OF_UNICODE michael@0: michael@0: JSString* str = JS_NewStringCopyN(cx, &p, 1); michael@0: if (!str) michael@0: return false; michael@0: michael@0: d.setString(str); michael@0: return true; michael@0: } michael@0: case nsXPTType::T_WCHAR : michael@0: { michael@0: jschar p = *static_cast(s); michael@0: michael@0: JSString* str = JS_NewUCStringCopyN(cx, &p, 1); michael@0: if (!str) michael@0: return false; michael@0: michael@0: d.setString(str); michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_JSVAL : michael@0: { michael@0: d.set(*static_cast(s)); michael@0: return JS_WrapValue(cx, d); michael@0: } michael@0: michael@0: case nsXPTType::T_VOID: michael@0: XPC_LOG_ERROR(("XPCConvert::NativeData2JS : void* params not supported")); michael@0: return false; michael@0: michael@0: case nsXPTType::T_IID: michael@0: { michael@0: nsID* iid2 = *static_cast(s); michael@0: if (!iid2) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: RootedObject scope(cx, JS::CurrentGlobalOrNull(cx)); michael@0: JSObject* obj = xpc_NewIDObject(cx, scope, *iid2); michael@0: if (!obj) michael@0: return false; michael@0: michael@0: d.setObject(*obj); michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_ASTRING: michael@0: // Fall through to T_DOMSTRING case michael@0: michael@0: case nsXPTType::T_DOMSTRING: michael@0: { michael@0: const nsAString* p = *static_cast(s); michael@0: if (!p || p->IsVoid()) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: nsStringBuffer* buf; michael@0: if (!XPCStringConvert::ReadableToJSVal(cx, *p, &buf, d)) michael@0: return false; michael@0: if (buf) michael@0: buf->AddRef(); michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_CHAR_STR: michael@0: { michael@0: const char* p = *static_cast(s); michael@0: if (!p) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: #ifdef STRICT_CHECK_OF_UNICODE michael@0: bool isAscii = true; michael@0: for (char* t = p; *t && isAscii; t++) { michael@0: if (ILLEGAL_CHAR_RANGE(*t)) michael@0: isAscii = false; michael@0: } michael@0: MOZ_ASSERT(isAscii, "passing non ASCII data"); michael@0: #endif // STRICT_CHECK_OF_UNICODE michael@0: michael@0: JSString* str = JS_NewStringCopyZ(cx, p); michael@0: if (!str) michael@0: return false; michael@0: michael@0: d.setString(str); michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_WCHAR_STR: michael@0: { michael@0: const jschar* p = *static_cast(s); michael@0: if (!p) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: JSString* str = JS_NewUCStringCopyZ(cx, p); michael@0: if (!str) michael@0: return false; michael@0: michael@0: d.setString(str); michael@0: return true; michael@0: } michael@0: case nsXPTType::T_UTF8STRING: michael@0: { michael@0: const nsACString* utf8String = *static_cast(s); michael@0: michael@0: if (!utf8String || utf8String->IsVoid()) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: if (utf8String->IsEmpty()) { michael@0: d.set(JS_GetEmptyStringValue(cx)); michael@0: return true; michael@0: } michael@0: michael@0: const uint32_t len = CalcUTF8ToUnicodeLength(*utf8String); michael@0: // The cString is not empty at this point, but the calculated michael@0: // UTF-16 length is zero, meaning no valid conversion exists. michael@0: if (!len) michael@0: return false; michael@0: michael@0: const size_t buffer_size = (len + 1) * sizeof(char16_t); michael@0: char16_t* buffer = michael@0: static_cast(JS_malloc(cx, buffer_size)); michael@0: if (!buffer) michael@0: return false; michael@0: michael@0: uint32_t copied; michael@0: if (!UTF8ToUnicodeBuffer(*utf8String, buffer, &copied) || michael@0: len != copied) { michael@0: // Copy or conversion during copy failed. Did not copy the michael@0: // whole string. michael@0: JS_free(cx, buffer); michael@0: return false; michael@0: } michael@0: michael@0: // JS_NewUCString takes ownership on success, i.e. a michael@0: // successful call will make it the responsiblity of the JS VM michael@0: // to free the buffer. michael@0: JSString* str = JS_NewUCString(cx, buffer, len); michael@0: if (!str) { michael@0: JS_free(cx, buffer); michael@0: return false; michael@0: } michael@0: michael@0: d.setString(str); michael@0: return true; michael@0: } michael@0: case nsXPTType::T_CSTRING: michael@0: { michael@0: const nsACString* cString = *static_cast(s); michael@0: michael@0: if (!cString || cString->IsVoid()) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: // c-strings (binary blobs) are deliberately not converted from michael@0: // UTF-8 to UTF-16. T_UTF8Sting is for UTF-8 encoded strings michael@0: // with automatic conversion. michael@0: JSString* str = JS_NewStringCopyN(cx, cString->Data(), michael@0: cString->Length()); michael@0: if (!str) michael@0: return false; michael@0: michael@0: d.setString(str); michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_INTERFACE: michael@0: case nsXPTType::T_INTERFACE_IS: michael@0: { michael@0: nsISupports* iface = *static_cast(s); michael@0: if (!iface) { michael@0: d.setNull(); michael@0: return true; michael@0: } michael@0: michael@0: if (iid->Equals(NS_GET_IID(nsIVariant))) { michael@0: nsCOMPtr variant = do_QueryInterface(iface); michael@0: if (!variant) michael@0: return false; michael@0: michael@0: return XPCVariant::VariantDataToJS(variant, michael@0: pErr, d); michael@0: } michael@0: michael@0: xpcObjectHelper helper(iface); michael@0: return NativeInterface2JSObject(d, nullptr, helper, iid, nullptr, true, pErr); michael@0: } michael@0: michael@0: default: michael@0: NS_ERROR("bad type"); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: michael@0: #ifdef DEBUG michael@0: static bool michael@0: CheckJSCharInCharRange(jschar c) michael@0: { michael@0: if (ILLEGAL_RANGE(c)) { michael@0: /* U+0080/U+0100 - U+FFFF data lost. */ michael@0: static const size_t MSG_BUF_SIZE = 64; michael@0: char msg[MSG_BUF_SIZE]; michael@0: JS_snprintf(msg, MSG_BUF_SIZE, "jschar out of char range; high bits of data lost: 0x%x", c); michael@0: NS_WARNING(msg); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: template michael@0: bool ConvertToPrimitive(JSContext *cx, HandleValue v, T *retval) michael@0: { michael@0: return ValueToPrimitive(cx, v, retval); michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::JSData2Native(void* d, HandleValue s, michael@0: const nsXPTType& type, michael@0: bool useAllocator, const nsID* iid, michael@0: nsresult* pErr) michael@0: { michael@0: NS_PRECONDITION(d, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS; michael@0: michael@0: switch (type.TagPart()) { michael@0: case nsXPTType::T_I8 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_I16 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_I32 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_I64 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_U8 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_U16 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_U32 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_U64 : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_FLOAT : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_DOUBLE : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_BOOL : michael@0: return ConvertToPrimitive(cx, s, static_cast(d)); michael@0: case nsXPTType::T_CHAR : michael@0: { michael@0: JSString* str = ToString(cx, s); michael@0: if (!str) { michael@0: return false; michael@0: } michael@0: size_t length; michael@0: const jschar* chars = JS_GetStringCharsAndLength(cx, str, &length); michael@0: if (!chars) { michael@0: return false; michael@0: } michael@0: jschar ch = length ? chars[0] : 0; michael@0: #ifdef DEBUG michael@0: CheckJSCharInCharRange(ch); michael@0: #endif michael@0: *((char*)d) = char(ch); michael@0: break; michael@0: } michael@0: case nsXPTType::T_WCHAR : michael@0: { michael@0: JSString* str; michael@0: if (!(str = ToString(cx, s))) { michael@0: return false; michael@0: } michael@0: size_t length; michael@0: const jschar* chars = JS_GetStringCharsAndLength(cx, str, &length); michael@0: if (!chars) { michael@0: return false; michael@0: } michael@0: if (length == 0) { michael@0: *((uint16_t*)d) = 0; michael@0: break; michael@0: } michael@0: *((uint16_t*)d) = uint16_t(chars[0]); michael@0: break; michael@0: } michael@0: case nsXPTType::T_JSVAL : michael@0: *((jsval*)d) = s; michael@0: break; michael@0: case nsXPTType::T_VOID: michael@0: XPC_LOG_ERROR(("XPCConvert::JSData2Native : void* params not supported")); michael@0: NS_ERROR("void* params not supported"); michael@0: return false; michael@0: case nsXPTType::T_IID: michael@0: { michael@0: const nsID* pid = nullptr; michael@0: michael@0: // There's no good reason to pass a null IID. michael@0: if (s.isNullOrUndefined()) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS; michael@0: return false; michael@0: } michael@0: michael@0: if (!s.isObject() || michael@0: (!(pid = xpc_JSObjectToID(cx, &s.toObject()))) || michael@0: (!(pid = (const nsID*) nsMemory::Clone(pid, sizeof(nsID))))) { michael@0: return false; michael@0: } michael@0: *((const nsID**)d) = pid; michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_ASTRING: michael@0: { michael@0: if (JSVAL_IS_VOID(s)) { michael@0: if (useAllocator) michael@0: *((const nsAString**)d) = &NullString(); michael@0: else michael@0: (**((nsAString**)d)).SetIsVoid(true); michael@0: return true; michael@0: } michael@0: // Fall through to T_DOMSTRING case. michael@0: } michael@0: case nsXPTType::T_DOMSTRING: michael@0: { michael@0: if (JSVAL_IS_NULL(s)) { michael@0: if (useAllocator) michael@0: *((const nsAString**)d) = &NullString(); michael@0: else michael@0: (**((nsAString**)d)).SetIsVoid(true); michael@0: return true; michael@0: } michael@0: size_t length = 0; michael@0: const char16_t* chars = nullptr; michael@0: JSString* str = nullptr; michael@0: if (!JSVAL_IS_VOID(s)) { michael@0: str = ToString(cx, s); michael@0: if (!str) michael@0: return false; michael@0: michael@0: chars = useAllocator ? JS_GetStringCharsZAndLength(cx, str, &length) michael@0: : JS_GetStringCharsAndLength(cx, str, &length); michael@0: if (!chars) michael@0: return false; michael@0: michael@0: if (!length) { michael@0: if (useAllocator) michael@0: *((const nsAString**)d) = &EmptyString(); michael@0: else michael@0: (**((nsAString**)d)).Truncate(); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: nsString* ws; michael@0: if (useAllocator) { michael@0: ws = nsXPConnect::GetRuntimeInstance()->NewShortLivedString(); michael@0: *((const nsString**)d) = ws; michael@0: } else { michael@0: ws = *((nsString**)d); michael@0: } michael@0: michael@0: if (!str) { michael@0: ws->AssignLiteral(MOZ_UTF16("undefined")); michael@0: } else if (XPCStringConvert::IsDOMString(str)) { michael@0: // The characters represent an existing nsStringBuffer that michael@0: // was shared by XPCStringConvert::ReadableToJSVal. michael@0: nsStringBuffer::FromData((void *)chars)->ToString(length, *ws); michael@0: } else if (XPCStringConvert::IsLiteral(str)) { michael@0: // The characters represent a literal char16_t string constant michael@0: // compiled into libxul, such as the string "undefined" above. michael@0: ws->AssignLiteral(chars, length); michael@0: } else if (useAllocator && STRING_TO_JSVAL(str) == s) { michael@0: // The JS string will exist over the function call. michael@0: // We don't need to copy the characters in this case. michael@0: ws->Rebind(chars, length); michael@0: } else { michael@0: ws->Assign(chars, length); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_CHAR_STR: michael@0: { michael@0: if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) { michael@0: *((char**)d) = nullptr; michael@0: return true; michael@0: } michael@0: michael@0: JSString* str = ToString(cx, s); michael@0: if (!str) { michael@0: return false; michael@0: } michael@0: #ifdef DEBUG michael@0: const jschar* chars=nullptr; michael@0: if (nullptr != (chars = JS_GetStringCharsZ(cx, str))) { michael@0: bool legalRange = true; michael@0: int len = JS_GetStringLength(str); michael@0: const jschar* t; michael@0: int32_t i=0; michael@0: for (t=chars; (i< len) && legalRange ; i++,t++) { michael@0: if (!CheckJSCharInCharRange(*t)) michael@0: break; michael@0: } michael@0: } michael@0: #endif // DEBUG michael@0: size_t length = JS_GetStringEncodingLength(cx, str); michael@0: if (length == size_t(-1)) { michael@0: return false; michael@0: } michael@0: char *buffer = static_cast(nsMemory::Alloc(length + 1)); michael@0: if (!buffer) { michael@0: return false; michael@0: } michael@0: JS_EncodeStringToBuffer(cx, str, buffer, length); michael@0: buffer[length] = '\0'; michael@0: *((void**)d) = buffer; michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_WCHAR_STR: michael@0: { michael@0: const jschar* chars=nullptr; michael@0: JSString* str; michael@0: michael@0: if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) { michael@0: *((jschar**)d) = nullptr; michael@0: return true; michael@0: } michael@0: michael@0: if (!(str = ToString(cx, s))) { michael@0: return false; michael@0: } michael@0: if (!(chars = JS_GetStringCharsZ(cx, str))) { michael@0: return false; michael@0: } michael@0: int len = JS_GetStringLength(str); michael@0: int byte_len = (len+1)*sizeof(jschar); michael@0: if (!(*((void**)d) = nsMemory::Alloc(byte_len))) { michael@0: // XXX should report error michael@0: return false; michael@0: } michael@0: jschar* destchars = *((jschar**)d); michael@0: memcpy(destchars, chars, byte_len); michael@0: destchars[len] = 0; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_UTF8STRING: michael@0: { michael@0: const jschar* chars; michael@0: size_t length; michael@0: JSString* str; michael@0: michael@0: if (JSVAL_IS_NULL(s) || JSVAL_IS_VOID(s)) { michael@0: if (useAllocator) { michael@0: *((const nsACString**)d) = &NullCString(); michael@0: } else { michael@0: nsCString* rs = *((nsCString**)d); michael@0: rs->SetIsVoid(true); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // The JS val is neither null nor void... michael@0: michael@0: if (!(str = ToString(cx, s))|| michael@0: !(chars = JS_GetStringCharsAndLength(cx, str, &length))) { michael@0: return false; michael@0: } michael@0: michael@0: if (!length) { michael@0: if (useAllocator) { michael@0: *((const nsACString**)d) = &EmptyCString(); michael@0: } else { michael@0: nsCString* rs = *((nsCString**)d); michael@0: rs->Truncate(); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: nsCString *rs; michael@0: if (useAllocator) { michael@0: // Use nsCString to enable sharing michael@0: rs = new nsCString(); michael@0: if (!rs) michael@0: return false; michael@0: michael@0: *((const nsCString**)d) = rs; michael@0: } else { michael@0: rs = *((nsCString**)d); michael@0: } michael@0: CopyUTF16toUTF8(Substring(chars, length), *rs); michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_CSTRING: michael@0: { michael@0: if (JSVAL_IS_NULL(s) || JSVAL_IS_VOID(s)) { michael@0: if (useAllocator) { michael@0: nsACString *rs = new nsCString(); michael@0: if (!rs) michael@0: return false; michael@0: michael@0: rs->SetIsVoid(true); michael@0: *((nsACString**)d) = rs; michael@0: } else { michael@0: nsACString* rs = *((nsACString**)d); michael@0: rs->Truncate(); michael@0: rs->SetIsVoid(true); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // The JS val is neither null nor void... michael@0: JSString* str = ToString(cx, s); michael@0: if (!str) { michael@0: return false; michael@0: } michael@0: michael@0: size_t length = JS_GetStringEncodingLength(cx, str); michael@0: if (length == size_t(-1)) { michael@0: return false; michael@0: } michael@0: michael@0: if (!length) { michael@0: if (useAllocator) { michael@0: *((const nsACString**)d) = &EmptyCString(); michael@0: } else { michael@0: nsCString* rs = *((nsCString**)d); michael@0: rs->Truncate(); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: nsACString *rs; michael@0: if (useAllocator) { michael@0: rs = new nsCString(); michael@0: if (!rs) michael@0: return false; michael@0: *((const nsACString**)d) = rs; michael@0: } else { michael@0: rs = *((nsACString**)d); michael@0: } michael@0: michael@0: rs->SetLength(uint32_t(length)); michael@0: if (rs->Length() != uint32_t(length)) { michael@0: return false; michael@0: } michael@0: JS_EncodeStringToBuffer(cx, str, rs->BeginWriting(), length); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_INTERFACE: michael@0: case nsXPTType::T_INTERFACE_IS: michael@0: { michael@0: MOZ_ASSERT(iid,"can't do interface conversions without iid"); michael@0: michael@0: if (iid->Equals(NS_GET_IID(nsIVariant))) { michael@0: nsCOMPtr variant = XPCVariant::newVariant(cx, s); michael@0: if (!variant) michael@0: return false; michael@0: michael@0: variant.forget(static_cast(d)); michael@0: return true; michael@0: } else if (iid->Equals(NS_GET_IID(nsIAtom)) && michael@0: JSVAL_IS_STRING(s)) { michael@0: // We're trying to pass a string as an nsIAtom. Let's atomize! michael@0: JSString* str = JSVAL_TO_STRING(s); michael@0: const char16_t* chars = JS_GetStringCharsZ(cx, str); michael@0: if (!chars) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS_NULL_REF; michael@0: return false; michael@0: } michael@0: uint32_t length = JS_GetStringLength(str); michael@0: nsCOMPtr atom = michael@0: NS_NewAtom(nsDependentSubstring(chars, chars + length)); michael@0: atom.forget((nsISupports**)d); michael@0: return true; michael@0: } michael@0: //else ... michael@0: michael@0: if (s.isNullOrUndefined()) { michael@0: *((nsISupports**)d) = nullptr; michael@0: return true; michael@0: } michael@0: michael@0: // only wrap JSObjects michael@0: if (!s.isObject()) { michael@0: if (pErr && s.isInt32() && 0 == s.toInt32()) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS_ZERO_ISNOT_NULL; michael@0: return false; michael@0: } michael@0: michael@0: RootedObject src(cx, &s.toObject()); michael@0: return JSObject2NativeInterface((void**)d, src, iid, nullptr, pErr); michael@0: } michael@0: default: michael@0: NS_ERROR("bad type"); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static inline bool michael@0: CreateHolderIfNeeded(HandleObject obj, MutableHandleValue d, michael@0: nsIXPConnectJSObjectHolder** dest) michael@0: { michael@0: if (dest) { michael@0: nsRefPtr objHolder = XPCJSObjectHolder::newHolder(obj); michael@0: if (!objHolder) michael@0: return false; michael@0: michael@0: objHolder.forget(dest); michael@0: } michael@0: michael@0: d.setObjectOrNull(obj); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: // static michael@0: bool michael@0: XPCConvert::NativeInterface2JSObject(MutableHandleValue d, michael@0: nsIXPConnectJSObjectHolder** dest, michael@0: xpcObjectHelper& aHelper, michael@0: const nsID* iid, michael@0: XPCNativeInterface** Interface, michael@0: bool allowNativeWrapper, michael@0: nsresult* pErr) michael@0: { michael@0: MOZ_ASSERT_IF(Interface, iid); michael@0: if (!iid) michael@0: iid = &NS_GET_IID(nsISupports); michael@0: michael@0: d.setNull(); michael@0: if (dest) michael@0: *dest = nullptr; michael@0: if (!aHelper.Object()) michael@0: return true; michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE; michael@0: michael@0: // We used to have code here that unwrapped and simply exposed the michael@0: // underlying JSObject. That caused anomolies when JSComponents were michael@0: // accessed from other JS code - they didn't act like other xpconnect michael@0: // wrapped components. So, instead, we create "double wrapped" objects michael@0: // (that means an XPCWrappedNative around an nsXPCWrappedJS). This isn't michael@0: // optimal -- we could detect this and roll the functionality into a michael@0: // single wrapper, but the current solution is good enough for now. michael@0: AutoJSContext cx; michael@0: XPCWrappedNativeScope* xpcscope = GetObjectScope(JS::CurrentGlobalOrNull(cx)); michael@0: if (!xpcscope) michael@0: return false; michael@0: michael@0: // First, see if this object supports the wrapper cache. michael@0: // Note: If |cache->IsDOMBinding()| is true, then it means that the object michael@0: // implementing it doesn't want a wrapped native as its JS Object, but michael@0: // instead it provides its own proxy object. In that case, the object michael@0: // to use is found as cache->GetWrapper(). If that is null, then the michael@0: // object will create (and fill the cache) from its WrapObject call. michael@0: nsWrapperCache *cache = aHelper.GetWrapperCache(); michael@0: michael@0: RootedObject flat(cx, cache ? cache->GetWrapper() : nullptr); michael@0: if (!flat && cache && cache->IsDOMBinding()) { michael@0: RootedObject global(cx, xpcscope->GetGlobalJSObject()); michael@0: js::AssertSameCompartment(cx, global); michael@0: flat = cache->WrapObject(cx); michael@0: if (!flat) michael@0: return false; michael@0: } michael@0: if (flat) { michael@0: if (allowNativeWrapper && !JS_WrapObject(cx, &flat)) michael@0: return false; michael@0: return CreateHolderIfNeeded(flat, d, dest); michael@0: } michael@0: michael@0: // Don't double wrap CPOWs. This is a temporary measure for compatibility michael@0: // with objects that don't provide necessary QIs (such as objects under michael@0: // the new DOM bindings). We expect the other side of the CPOW to have michael@0: // the appropriate wrappers in place. michael@0: RootedObject cpow(cx, UnwrapNativeCPOW(aHelper.Object())); michael@0: if (cpow) { michael@0: if (!JS_WrapObject(cx, &cpow)) michael@0: return false; michael@0: d.setObject(*cpow); michael@0: return true; michael@0: } michael@0: michael@0: // We can't simply construct a slim wrapper. Go ahead and create an michael@0: // XPCWrappedNative for this object. At this point, |flat| could be michael@0: // non-null, meaning that either we already have a wrapped native from michael@0: // the cache (which might need to be QI'd to the new interface) or that michael@0: // we found a slim wrapper that we'll have to morph. michael@0: AutoMarkingNativeInterfacePtr iface(cx); michael@0: if (iid) { michael@0: if (Interface) michael@0: iface = *Interface; michael@0: michael@0: if (!iface) { michael@0: iface = XPCNativeInterface::GetNewOrUsed(iid); michael@0: if (!iface) michael@0: return false; michael@0: michael@0: if (Interface) michael@0: *Interface = iface; michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(!flat || IS_WN_REFLECTOR(flat), "What kind of wrapper is this?"); michael@0: michael@0: nsresult rv; michael@0: XPCWrappedNative* wrapper; michael@0: nsRefPtr strongWrapper; michael@0: if (!flat) { michael@0: rv = XPCWrappedNative::GetNewOrUsed(aHelper, xpcscope, iface, michael@0: getter_AddRefs(strongWrapper)); michael@0: michael@0: wrapper = strongWrapper; michael@0: } else { michael@0: MOZ_ASSERT(IS_WN_REFLECTOR(flat)); michael@0: michael@0: wrapper = XPCWrappedNative::Get(flat); michael@0: michael@0: // If asked to return the wrapper we'll return a strong reference, michael@0: // otherwise we'll just return its JSObject in d (which should be michael@0: // rooted in that case). michael@0: if (dest) michael@0: strongWrapper = wrapper; michael@0: if (iface) michael@0: wrapper->FindTearOff(iface, false, &rv); michael@0: else michael@0: rv = NS_OK; michael@0: } michael@0: michael@0: if (NS_FAILED(rv) && pErr) michael@0: *pErr = rv; michael@0: michael@0: // If creating the wrapped native failed, then return early. michael@0: if (NS_FAILED(rv) || !wrapper) michael@0: return false; michael@0: michael@0: // If we're not creating security wrappers, we can return the michael@0: // XPCWrappedNative as-is here. michael@0: flat = wrapper->GetFlatJSObject(); michael@0: jsval v = OBJECT_TO_JSVAL(flat); michael@0: if (!allowNativeWrapper) { michael@0: d.set(v); michael@0: if (dest) michael@0: strongWrapper.forget(dest); michael@0: if (pErr) michael@0: *pErr = NS_OK; michael@0: return true; michael@0: } michael@0: michael@0: // The call to wrap here handles both cross-compartment and same-compartment michael@0: // security wrappers. michael@0: RootedObject original(cx, flat); michael@0: if (!JS_WrapObject(cx, &flat)) michael@0: return false; michael@0: michael@0: d.setObjectOrNull(flat); michael@0: michael@0: if (dest) { michael@0: // The strongWrapper still holds the original flat object. michael@0: if (flat == original) { michael@0: strongWrapper.forget(dest); michael@0: } else { michael@0: nsRefPtr objHolder = michael@0: XPCJSObjectHolder::newHolder(flat); michael@0: if (!objHolder) michael@0: return false; michael@0: michael@0: objHolder.forget(dest); michael@0: } michael@0: } michael@0: michael@0: if (pErr) michael@0: *pErr = NS_OK; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::JSObject2NativeInterface(void** dest, HandleObject src, michael@0: const nsID* iid, michael@0: nsISupports* aOuter, michael@0: nsresult* pErr) michael@0: { michael@0: MOZ_ASSERT(dest, "bad param"); michael@0: MOZ_ASSERT(src, "bad param"); michael@0: MOZ_ASSERT(iid, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: JSAutoCompartment ac(cx, src); michael@0: michael@0: *dest = nullptr; michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS; michael@0: michael@0: nsISupports* iface; michael@0: michael@0: if (!aOuter) { michael@0: // Note that if we have a non-null aOuter then it means that we are michael@0: // forcing the creation of a wrapper even if the object *is* a michael@0: // wrappedNative or other wise has 'nsISupportness'. michael@0: // This allows wrapJSAggregatedToNative to work. michael@0: michael@0: // If we're looking at a security wrapper, see now if we're allowed to michael@0: // pass it to C++. If we are, then fall through to the code below. If michael@0: // we aren't, throw an exception eagerly. michael@0: // michael@0: // NB: It's very important that we _don't_ unwrap in the aOuter case, michael@0: // because the caller may explicitly want to create the XPCWrappedJS michael@0: // around a security wrapper. XBL does this with Xrays from the XBL michael@0: // scope - see nsBindingManager::GetBindingImplementation. michael@0: JSObject* inner = js::CheckedUnwrap(src, /* stopAtOuter = */ false); michael@0: michael@0: // Hack - For historical reasons, wrapped chrome JS objects have been michael@0: // passable as native interfaces. We'd like to fix this, but it michael@0: // involves fixing the contacts API and PeerConnection to stop using michael@0: // COWs. This needs to happen, but for now just preserve the old michael@0: // behavior. michael@0: // michael@0: // Note that there is an identical hack in getWrapper which should be michael@0: // removed if this one is. michael@0: if (!inner && MOZ_UNLIKELY(xpc::WrapperFactory::IsCOW(src))) michael@0: inner = js::UncheckedUnwrap(src); michael@0: if (!inner) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_SECURITY_MANAGER_VETO; michael@0: return false; michael@0: } michael@0: michael@0: // Is this really a native xpcom object with a wrapper? michael@0: XPCWrappedNative* wrappedNative = nullptr; michael@0: if (IS_WN_REFLECTOR(inner)) michael@0: wrappedNative = XPCWrappedNative::Get(inner); michael@0: if (wrappedNative) { michael@0: iface = wrappedNative->GetIdentityObject(); michael@0: return NS_SUCCEEDED(iface->QueryInterface(*iid, dest)); michael@0: } michael@0: // else... michael@0: michael@0: // Deal with slim wrappers here. michael@0: if (GetISupportsFromJSObject(inner ? inner : src, &iface)) { michael@0: if (iface) michael@0: return NS_SUCCEEDED(iface->QueryInterface(*iid, dest)); michael@0: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // else... michael@0: michael@0: nsXPCWrappedJS* wrapper; michael@0: nsresult rv = nsXPCWrappedJS::GetNewOrUsed(src, *iid, &wrapper); michael@0: if (pErr) michael@0: *pErr = rv; michael@0: if (NS_SUCCEEDED(rv) && wrapper) { michael@0: // If the caller wanted to aggregate this JS object to a native, michael@0: // attach it to the wrapper. Note that we allow a maximum of one michael@0: // aggregated native for a given XPCWrappedJS. michael@0: if (aOuter) michael@0: wrapper->SetAggregatedNativeObject(aOuter); michael@0: michael@0: // We need to go through the QueryInterface logic to make this return michael@0: // the right thing for the various 'special' interfaces; e.g. michael@0: // nsIPropertyBag. We must use AggregatedQueryInterface in cases where michael@0: // there is an outer to avoid nasty recursion. michael@0: rv = aOuter ? wrapper->AggregatedQueryInterface(*iid, dest) : michael@0: wrapper->QueryInterface(*iid, dest); michael@0: if (pErr) michael@0: *pErr = rv; michael@0: NS_RELEASE(wrapper); michael@0: return NS_SUCCEEDED(rv); michael@0: } michael@0: michael@0: // else... michael@0: return false; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: /***************************************************************************/ michael@0: michael@0: // static michael@0: nsresult michael@0: XPCConvert::ConstructException(nsresult rv, const char* message, michael@0: const char* ifaceName, const char* methodName, michael@0: nsISupports* data, michael@0: nsIException** exceptn, michael@0: JSContext* cx, michael@0: jsval* jsExceptionPtr) michael@0: { michael@0: MOZ_ASSERT(!cx == !jsExceptionPtr, "Expected cx and jsExceptionPtr to cooccur."); michael@0: michael@0: static const char format[] = "\'%s\' when calling method: [%s::%s]"; michael@0: const char * msg = message; michael@0: nsXPIDLString xmsg; michael@0: nsAutoCString sxmsg; michael@0: michael@0: nsCOMPtr errorObject = do_QueryInterface(data); michael@0: if (errorObject) { michael@0: if (NS_SUCCEEDED(errorObject->GetMessageMoz(getter_Copies(xmsg)))) { michael@0: CopyUTF16toUTF8(xmsg, sxmsg); michael@0: msg = sxmsg.get(); michael@0: } michael@0: } michael@0: if (!msg) michael@0: if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &msg) || ! msg) michael@0: msg = ""; michael@0: michael@0: nsCString msgStr(msg); michael@0: if (ifaceName && methodName) michael@0: msgStr.AppendPrintf(format, msg, ifaceName, methodName); michael@0: michael@0: nsRefPtr e = new Exception(msgStr, rv, EmptyCString(), nullptr, data); michael@0: michael@0: if (cx && jsExceptionPtr) { michael@0: e->StowJSVal(*jsExceptionPtr); michael@0: } michael@0: michael@0: e.forget(exceptn); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /********************************/ michael@0: michael@0: class MOZ_STACK_CLASS AutoExceptionRestorer michael@0: { michael@0: public: michael@0: AutoExceptionRestorer(JSContext *cx, Value v) michael@0: : mContext(cx), tvr(cx, v) michael@0: { michael@0: JS_ClearPendingException(mContext); michael@0: } michael@0: michael@0: ~AutoExceptionRestorer() michael@0: { michael@0: JS_SetPendingException(mContext, tvr); michael@0: } michael@0: michael@0: private: michael@0: JSContext * const mContext; michael@0: RootedValue tvr; michael@0: }; michael@0: michael@0: // static michael@0: nsresult michael@0: XPCConvert::JSValToXPCException(MutableHandleValue s, michael@0: const char* ifaceName, michael@0: const char* methodName, michael@0: nsIException** exceptn) michael@0: { michael@0: AutoJSContext cx; michael@0: AutoExceptionRestorer aer(cx, s); michael@0: michael@0: if (!JSVAL_IS_PRIMITIVE(s)) { michael@0: // we have a JSObject michael@0: RootedObject obj(cx, JSVAL_TO_OBJECT(s)); michael@0: michael@0: if (!obj) { michael@0: NS_ERROR("when is an object not an object?"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // is this really a native xpcom object with a wrapper? michael@0: JSObject *unwrapped = js::CheckedUnwrap(obj, /* stopAtOuter = */ false); michael@0: if (!unwrapped) michael@0: return NS_ERROR_XPC_SECURITY_MANAGER_VETO; michael@0: XPCWrappedNative* wrapper = IS_WN_REFLECTOR(unwrapped) ? XPCWrappedNative::Get(unwrapped) michael@0: : nullptr; michael@0: if (wrapper) { michael@0: nsISupports* supports = wrapper->GetIdentityObject(); michael@0: nsCOMPtr iface = do_QueryInterface(supports); michael@0: if (iface) { michael@0: // just pass through the exception (with extra ref and all) michael@0: nsCOMPtr temp = iface; michael@0: temp.forget(exceptn); michael@0: return NS_OK; michael@0: } else { michael@0: // it is a wrapped native, but not an exception! michael@0: return ConstructException(NS_ERROR_XPC_JS_THREW_NATIVE_OBJECT, michael@0: nullptr, ifaceName, methodName, supports, michael@0: exceptn, nullptr, nullptr); michael@0: } michael@0: } else { michael@0: // It is a JSObject, but not a wrapped native... michael@0: michael@0: // If it is an engine Error with an error report then let's michael@0: // extract the report and build an xpcexception from that michael@0: const JSErrorReport* report; michael@0: if (nullptr != (report = JS_ErrorFromException(cx, obj))) { michael@0: JSAutoByteString message; michael@0: JSString* str; michael@0: if (nullptr != (str = ToString(cx, s))) michael@0: message.encodeLatin1(cx, str); michael@0: return JSErrorToXPCException(message.ptr(), ifaceName, michael@0: methodName, report, exceptn); michael@0: } michael@0: michael@0: michael@0: bool found; michael@0: michael@0: // heuristic to see if it might be usable as an xpcexception michael@0: if (!JS_HasProperty(cx, obj, "message", &found)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (found && !JS_HasProperty(cx, obj, "result", &found)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (found) { michael@0: // lets try to build a wrapper around the JSObject michael@0: nsXPCWrappedJS* jswrapper; michael@0: nsresult rv = michael@0: nsXPCWrappedJS::GetNewOrUsed(obj, NS_GET_IID(nsIException), &jswrapper); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: *exceptn = static_cast(jswrapper->GetXPTCStub()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // XXX we should do a check against 'js_ErrorClass' here and michael@0: // do the right thing - even though it has no JSErrorReport, michael@0: // The fact that it is a JSError exceptions means we can extract michael@0: // particular info and our 'result' should reflect that. michael@0: michael@0: // otherwise we'll just try to convert it to a string michael@0: michael@0: JSString* str = ToString(cx, s); michael@0: if (!str) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: JSAutoByteString strBytes(cx, str); michael@0: if (!strBytes) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: return ConstructException(NS_ERROR_XPC_JS_THREW_JS_OBJECT, michael@0: strBytes.ptr(), ifaceName, methodName, michael@0: nullptr, exceptn, cx, s.address()); michael@0: } michael@0: } michael@0: michael@0: if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) { michael@0: return ConstructException(NS_ERROR_XPC_JS_THREW_NULL, michael@0: nullptr, ifaceName, methodName, nullptr, michael@0: exceptn, cx, s.address()); michael@0: } michael@0: michael@0: if (JSVAL_IS_NUMBER(s)) { michael@0: // lets see if it looks like an nsresult michael@0: nsresult rv; michael@0: double number; michael@0: bool isResult = false; michael@0: michael@0: if (JSVAL_IS_INT(s)) { michael@0: rv = (nsresult) JSVAL_TO_INT(s); michael@0: if (NS_FAILED(rv)) michael@0: isResult = true; michael@0: else michael@0: number = (double) JSVAL_TO_INT(s); michael@0: } else { michael@0: number = JSVAL_TO_DOUBLE(s); michael@0: if (number > 0.0 && michael@0: number < (double)0xffffffff && michael@0: 0.0 == fmod(number,1)) { michael@0: // Visual Studio 9 doesn't allow casting directly from a michael@0: // double to an enumeration type, contrary to 5.2.9(10) of michael@0: // C++11, so add an intermediate cast. michael@0: rv = (nsresult)(uint32_t) number; michael@0: if (NS_FAILED(rv)) michael@0: isResult = true; michael@0: } michael@0: } michael@0: michael@0: if (isResult) michael@0: return ConstructException(rv, nullptr, ifaceName, methodName, michael@0: nullptr, exceptn, cx, s.address()); michael@0: else { michael@0: // XXX all this nsISupportsDouble code seems a little redundant michael@0: // now that we're storing the jsval in the exception... michael@0: nsISupportsDouble* data; michael@0: nsCOMPtr cm; michael@0: if (NS_FAILED(NS_GetComponentManager(getter_AddRefs(cm))) || !cm || michael@0: NS_FAILED(cm->CreateInstanceByContractID(NS_SUPPORTS_DOUBLE_CONTRACTID, michael@0: nullptr, michael@0: NS_GET_IID(nsISupportsDouble), michael@0: (void**)&data))) michael@0: return NS_ERROR_FAILURE; michael@0: data->SetData(number); michael@0: rv = ConstructException(NS_ERROR_XPC_JS_THREW_NUMBER, nullptr, michael@0: ifaceName, methodName, data, exceptn, cx, s.address()); michael@0: NS_RELEASE(data); michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: // otherwise we'll just try to convert it to a string michael@0: // Note: e.g., bools get converted to JSStrings by this code. michael@0: michael@0: JSString* str = ToString(cx, s); michael@0: if (str) { michael@0: JSAutoByteString strBytes(cx, str); michael@0: if (!!strBytes) { michael@0: return ConstructException(NS_ERROR_XPC_JS_THREW_STRING, michael@0: strBytes.ptr(), ifaceName, methodName, michael@0: nullptr, exceptn, cx, s.address()); michael@0: } michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: /********************************/ michael@0: michael@0: // static michael@0: nsresult michael@0: XPCConvert::JSErrorToXPCException(const char* message, michael@0: const char* ifaceName, michael@0: const char* methodName, michael@0: const JSErrorReport* report, michael@0: nsIException** exceptn) michael@0: { michael@0: AutoJSContext cx; michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: nsRefPtr data; michael@0: if (report) { michael@0: nsAutoString bestMessage; michael@0: if (report && report->ucmessage) { michael@0: bestMessage = static_cast(report->ucmessage); michael@0: } else if (message) { michael@0: CopyASCIItoUTF16(message, bestMessage); michael@0: } else { michael@0: bestMessage.AssignLiteral("JavaScript Error"); michael@0: } michael@0: michael@0: const char16_t* uclinebuf = michael@0: static_cast(report->uclinebuf); michael@0: michael@0: data = new nsScriptError(); michael@0: data->InitWithWindowID( michael@0: bestMessage, michael@0: NS_ConvertASCIItoUTF16(report->filename), michael@0: uclinebuf ? nsDependentString(uclinebuf) : EmptyString(), michael@0: report->lineno, michael@0: report->uctokenptr - report->uclinebuf, report->flags, michael@0: NS_LITERAL_CSTRING("XPConnect JavaScript"), michael@0: nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx)); michael@0: } michael@0: michael@0: if (data) { michael@0: nsAutoCString formattedMsg; michael@0: data->ToString(formattedMsg); michael@0: michael@0: rv = ConstructException(NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS, michael@0: formattedMsg.get(), ifaceName, methodName, michael@0: static_cast(data.get()), michael@0: exceptn, nullptr, nullptr); michael@0: } else { michael@0: rv = ConstructException(NS_ERROR_XPC_JAVASCRIPT_ERROR, michael@0: nullptr, ifaceName, methodName, nullptr, michael@0: exceptn, nullptr, nullptr); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /***************************************************************************/ michael@0: michael@0: // array fun... michael@0: michael@0: #ifdef POPULATE michael@0: #undef POPULATE michael@0: #endif michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::NativeArray2JS(MutableHandleValue d, const void** s, michael@0: const nsXPTType& type, const nsID* iid, michael@0: uint32_t count, nsresult* pErr) michael@0: { michael@0: NS_PRECONDITION(s, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: michael@0: // XXX add support for putting chars in a string rather than an array michael@0: michael@0: // XXX add support to indicate *which* array element was not convertable michael@0: michael@0: RootedObject array(cx, JS_NewArrayObject(cx, count)); michael@0: if (!array) michael@0: return false; michael@0: michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE; michael@0: michael@0: uint32_t i; michael@0: RootedValue current(cx, JSVAL_NULL); michael@0: michael@0: #define POPULATE(_t) \ michael@0: PR_BEGIN_MACRO \ michael@0: for (i = 0; i < count; i++) { \ michael@0: if (!NativeData2JS(¤t, ((_t*)*s)+i, type, iid, pErr) || \ michael@0: !JS_SetElement(cx, array, i, current)) \ michael@0: goto failure; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: // XXX check IsPtr - esp. to handle array of nsID (as opposed to nsID*) michael@0: michael@0: switch (type.TagPart()) { michael@0: case nsXPTType::T_I8 : POPULATE(int8_t); break; michael@0: case nsXPTType::T_I16 : POPULATE(int16_t); break; michael@0: case nsXPTType::T_I32 : POPULATE(int32_t); break; michael@0: case nsXPTType::T_I64 : POPULATE(int64_t); break; michael@0: case nsXPTType::T_U8 : POPULATE(uint8_t); break; michael@0: case nsXPTType::T_U16 : POPULATE(uint16_t); break; michael@0: case nsXPTType::T_U32 : POPULATE(uint32_t); break; michael@0: case nsXPTType::T_U64 : POPULATE(uint64_t); break; michael@0: case nsXPTType::T_FLOAT : POPULATE(float); break; michael@0: case nsXPTType::T_DOUBLE : POPULATE(double); break; michael@0: case nsXPTType::T_BOOL : POPULATE(bool); break; michael@0: case nsXPTType::T_CHAR : POPULATE(char); break; michael@0: case nsXPTType::T_WCHAR : POPULATE(jschar); break; michael@0: case nsXPTType::T_VOID : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_IID : POPULATE(nsID*); break; michael@0: case nsXPTType::T_DOMSTRING : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_CHAR_STR : POPULATE(char*); break; michael@0: case nsXPTType::T_WCHAR_STR : POPULATE(jschar*); break; michael@0: case nsXPTType::T_INTERFACE : POPULATE(nsISupports*); break; michael@0: case nsXPTType::T_INTERFACE_IS : POPULATE(nsISupports*); break; michael@0: case nsXPTType::T_UTF8STRING : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_CSTRING : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_ASTRING : NS_ERROR("bad type"); goto failure; michael@0: default : NS_ERROR("bad type"); goto failure; michael@0: } michael@0: michael@0: if (pErr) michael@0: *pErr = NS_OK; michael@0: d.setObject(*array); michael@0: return true; michael@0: michael@0: failure: michael@0: return false; michael@0: michael@0: #undef POPULATE michael@0: } michael@0: michael@0: michael@0: michael@0: // Check that the tag part of the type matches the type michael@0: // of the array. If the check succeeds, check that the size michael@0: // of the output does not exceed UINT32_MAX bytes. Allocate michael@0: // the memory and copy the elements by memcpy. michael@0: static bool michael@0: CheckTargetAndPopulate(const nsXPTType& type, michael@0: uint8_t requiredType, michael@0: size_t typeSize, michael@0: uint32_t count, michael@0: JSObject* tArr, michael@0: void** output, michael@0: nsresult* pErr) michael@0: { michael@0: // Check that the element type expected by the interface matches michael@0: // the type of the elements in the typed array exactly, including michael@0: // signedness. michael@0: if (type.TagPart() != requiredType) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // Calulate the maximum number of elements that can fit in michael@0: // UINT32_MAX bytes. michael@0: size_t max = UINT32_MAX / typeSize; michael@0: michael@0: // This could overflow on 32-bit systems so check max first. michael@0: size_t byteSize = count * typeSize; michael@0: if (count > max || !(*output = nsMemory::Alloc(byteSize))) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: memcpy(*output, JS_GetArrayBufferViewData(tArr), byteSize); michael@0: return true; michael@0: } michael@0: michael@0: // Fast conversion of typed arrays to native using memcpy. michael@0: // No float or double canonicalization is done. Called by michael@0: // JSarray2Native whenever a TypedArray is met. ArrayBuffers michael@0: // are not accepted; create a properly typed array view on them michael@0: // first. The element type of array must match the XPCOM michael@0: // type in size, type and signedness exactly. As an exception, michael@0: // Uint8ClampedArray is allowed for arrays of uint8_t. DataViews michael@0: // are not supported. michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::JSTypedArray2Native(void** d, michael@0: JSObject* jsArray, michael@0: uint32_t count, michael@0: const nsXPTType& type, michael@0: nsresult* pErr) michael@0: { michael@0: MOZ_ASSERT(jsArray, "bad param"); michael@0: MOZ_ASSERT(d, "bad param"); michael@0: MOZ_ASSERT(JS_IsTypedArrayObject(jsArray), "not a typed array"); michael@0: michael@0: // Check the actual length of the input array against the michael@0: // given size_is. michael@0: uint32_t len = JS_GetTypedArrayLength(jsArray); michael@0: if (len < count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void* output = nullptr; michael@0: michael@0: switch (JS_GetArrayBufferViewType(jsArray)) { michael@0: case js::ArrayBufferView::TYPE_INT8: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_I8, type, michael@0: sizeof(int8_t), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_UINT8: michael@0: case js::ArrayBufferView::TYPE_UINT8_CLAMPED: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_U8, type, michael@0: sizeof(uint8_t), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_INT16: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_I16, type, michael@0: sizeof(int16_t), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_UINT16: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_U16, type, michael@0: sizeof(uint16_t), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_INT32: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_I32, type, michael@0: sizeof(int32_t), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_UINT32: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_U32, type, michael@0: sizeof(uint32_t), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_FLOAT32: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_FLOAT, type, michael@0: sizeof(float), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case js::ArrayBufferView::TYPE_FLOAT64: michael@0: if (!CheckTargetAndPopulate(nsXPTType::T_DOUBLE, type, michael@0: sizeof(double), count, michael@0: jsArray, &output, pErr)) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: // Yet another array type was defined? It is not supported yet... michael@0: default: michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: *d = output; michael@0: if (pErr) michael@0: *pErr = NS_OK; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::JSArray2Native(void** d, HandleValue s, michael@0: uint32_t count, const nsXPTType& type, michael@0: const nsID* iid, nsresult* pErr) michael@0: { michael@0: MOZ_ASSERT(d, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: michael@0: // XXX add support for getting chars from strings michael@0: michael@0: // XXX add support to indicate *which* array element was not convertable michael@0: michael@0: if (s.isNullOrUndefined()) { michael@0: if (0 != count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY; michael@0: return false; michael@0: } michael@0: michael@0: *d = nullptr; michael@0: return true; michael@0: } michael@0: michael@0: if (!s.isObject()) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_CANT_CONVERT_PRIMITIVE_TO_ARRAY; michael@0: return false; michael@0: } michael@0: michael@0: RootedObject jsarray(cx, &s.toObject()); michael@0: michael@0: // If this is a typed array, then try a fast conversion with memcpy. michael@0: if (JS_IsTypedArrayObject(jsarray)) { michael@0: return JSTypedArray2Native(d, jsarray, count, type, pErr); michael@0: } michael@0: michael@0: if (!JS_IsArrayObject(cx, jsarray)) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_CANT_CONVERT_OBJECT_TO_ARRAY; michael@0: return false; michael@0: } michael@0: michael@0: uint32_t len; michael@0: if (!JS_GetArrayLength(cx, jsarray, &len) || len < count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY; michael@0: return false; michael@0: } michael@0: michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_JS; michael@0: michael@0: #define POPULATE(_mode, _t) \ michael@0: PR_BEGIN_MACRO \ michael@0: cleanupMode = _mode; \ michael@0: size_t max = UINT32_MAX / sizeof(_t); \ michael@0: if (count > max || \ michael@0: nullptr == (array = nsMemory::Alloc(count * sizeof(_t)))) { \ michael@0: if (pErr) \ michael@0: *pErr = NS_ERROR_OUT_OF_MEMORY; \ michael@0: goto failure; \ michael@0: } \ michael@0: for (initedCount = 0; initedCount < count; initedCount++) { \ michael@0: if (!JS_GetElement(cx, jsarray, initedCount, ¤t) || \ michael@0: !JSData2Native(((_t*)array)+initedCount, current, type, \ michael@0: true, iid, pErr)) \ michael@0: goto failure; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: // No Action, FRee memory, RElease object michael@0: enum CleanupMode {na, fr, re}; michael@0: michael@0: CleanupMode cleanupMode; michael@0: michael@0: void *array = nullptr; michael@0: uint32_t initedCount; michael@0: RootedValue current(cx); michael@0: michael@0: // XXX check IsPtr - esp. to handle array of nsID (as opposed to nsID*) michael@0: // XXX make extra space at end of char* and wchar* and null termintate michael@0: michael@0: switch (type.TagPart()) { michael@0: case nsXPTType::T_I8 : POPULATE(na, int8_t); break; michael@0: case nsXPTType::T_I16 : POPULATE(na, int16_t); break; michael@0: case nsXPTType::T_I32 : POPULATE(na, int32_t); break; michael@0: case nsXPTType::T_I64 : POPULATE(na, int64_t); break; michael@0: case nsXPTType::T_U8 : POPULATE(na, uint8_t); break; michael@0: case nsXPTType::T_U16 : POPULATE(na, uint16_t); break; michael@0: case nsXPTType::T_U32 : POPULATE(na, uint32_t); break; michael@0: case nsXPTType::T_U64 : POPULATE(na, uint64_t); break; michael@0: case nsXPTType::T_FLOAT : POPULATE(na, float); break; michael@0: case nsXPTType::T_DOUBLE : POPULATE(na, double); break; michael@0: case nsXPTType::T_BOOL : POPULATE(na, bool); break; michael@0: case nsXPTType::T_CHAR : POPULATE(na, char); break; michael@0: case nsXPTType::T_WCHAR : POPULATE(na, jschar); break; michael@0: case nsXPTType::T_VOID : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_IID : POPULATE(fr, nsID*); break; michael@0: case nsXPTType::T_DOMSTRING : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_CHAR_STR : POPULATE(fr, char*); break; michael@0: case nsXPTType::T_WCHAR_STR : POPULATE(fr, jschar*); break; michael@0: case nsXPTType::T_INTERFACE : POPULATE(re, nsISupports*); break; michael@0: case nsXPTType::T_INTERFACE_IS : POPULATE(re, nsISupports*); break; michael@0: case nsXPTType::T_UTF8STRING : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_CSTRING : NS_ERROR("bad type"); goto failure; michael@0: case nsXPTType::T_ASTRING : NS_ERROR("bad type"); goto failure; michael@0: default : NS_ERROR("bad type"); goto failure; michael@0: } michael@0: michael@0: *d = array; michael@0: if (pErr) michael@0: *pErr = NS_OK; michael@0: return true; michael@0: michael@0: failure: michael@0: // we may need to cleanup the partially filled array of converted stuff michael@0: if (array) { michael@0: if (cleanupMode == re) { michael@0: nsISupports** a = (nsISupports**) array; michael@0: for (uint32_t i = 0; i < initedCount; i++) { michael@0: nsISupports* p = a[i]; michael@0: NS_IF_RELEASE(p); michael@0: } michael@0: } else if (cleanupMode == fr) { michael@0: void** a = (void**) array; michael@0: for (uint32_t i = 0; i < initedCount; i++) { michael@0: void* p = a[i]; michael@0: if (p) nsMemory::Free(p); michael@0: } michael@0: } michael@0: nsMemory::Free(array); michael@0: } michael@0: michael@0: return false; michael@0: michael@0: #undef POPULATE michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::NativeStringWithSize2JS(MutableHandleValue d, const void* s, michael@0: const nsXPTType& type, michael@0: uint32_t count, michael@0: nsresult* pErr) michael@0: { michael@0: NS_PRECONDITION(s, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE; michael@0: michael@0: switch (type.TagPart()) { michael@0: case nsXPTType::T_PSTRING_SIZE_IS: michael@0: { michael@0: char* p = *((char**)s); michael@0: if (!p) michael@0: break; michael@0: JSString* str; michael@0: if (!(str = JS_NewStringCopyN(cx, p, count))) michael@0: return false; michael@0: d.setString(str); michael@0: break; michael@0: } michael@0: case nsXPTType::T_PWSTRING_SIZE_IS: michael@0: { michael@0: jschar* p = *((jschar**)s); michael@0: if (!p) michael@0: break; michael@0: JSString* str; michael@0: if (!(str = JS_NewUCStringCopyN(cx, p, count))) michael@0: return false; michael@0: d.setString(str); michael@0: break; michael@0: } michael@0: default: michael@0: XPC_LOG_ERROR(("XPCConvert::NativeStringWithSize2JS : unsupported type")); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: XPCConvert::JSStringWithSize2Native(void* d, HandleValue s, michael@0: uint32_t count, const nsXPTType& type, michael@0: nsresult* pErr) michael@0: { michael@0: NS_PRECONDITION(!JSVAL_IS_NULL(s), "bad param"); michael@0: NS_PRECONDITION(d, "bad param"); michael@0: michael@0: AutoJSContext cx; michael@0: uint32_t len; michael@0: michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE; michael@0: michael@0: switch (type.TagPart()) { michael@0: case nsXPTType::T_PSTRING_SIZE_IS: michael@0: { michael@0: if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) { michael@0: if (0 != count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING; michael@0: return false; michael@0: } michael@0: if (0 != count) { michael@0: len = (count + 1) * sizeof(char); michael@0: if (!(*((void**)d) = nsMemory::Alloc(len))) michael@0: return false; michael@0: return true; michael@0: } michael@0: // else ... michael@0: michael@0: *((char**)d) = nullptr; michael@0: return true; michael@0: } michael@0: michael@0: JSString* str = ToString(cx, s); michael@0: if (!str) { michael@0: return false; michael@0: } michael@0: michael@0: size_t length = JS_GetStringEncodingLength(cx, str); michael@0: if (length == size_t(-1)) { michael@0: return false; michael@0: } michael@0: if (length > count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING; michael@0: return false; michael@0: } michael@0: len = uint32_t(length); michael@0: michael@0: if (len < count) michael@0: len = count; michael@0: michael@0: uint32_t alloc_len = (len + 1) * sizeof(char); michael@0: char *buffer = static_cast(nsMemory::Alloc(alloc_len)); michael@0: if (!buffer) { michael@0: return false; michael@0: } michael@0: JS_EncodeStringToBuffer(cx, str, buffer, len); michael@0: buffer[len] = '\0'; michael@0: *((char**)d) = buffer; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: case nsXPTType::T_PWSTRING_SIZE_IS: michael@0: { michael@0: const jschar* chars=nullptr; michael@0: JSString* str; michael@0: michael@0: if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) { michael@0: if (0 != count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING; michael@0: return false; michael@0: } michael@0: michael@0: if (0 != count) { michael@0: len = (count + 1) * sizeof(jschar); michael@0: if (!(*((void**)d) = nsMemory::Alloc(len))) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: // else ... michael@0: *((const jschar**)d) = nullptr; michael@0: return true; michael@0: } michael@0: michael@0: if (!(str = ToString(cx, s))) { michael@0: return false; michael@0: } michael@0: michael@0: len = JS_GetStringLength(str); michael@0: if (len > count) { michael@0: if (pErr) michael@0: *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING; michael@0: return false; michael@0: } michael@0: if (len < count) michael@0: len = count; michael@0: michael@0: if (!(chars = JS_GetStringCharsZ(cx, str))) { michael@0: return false; michael@0: } michael@0: uint32_t alloc_len = (len + 1) * sizeof(jschar); michael@0: if (!(*((void**)d) = nsMemory::Alloc(alloc_len))) { michael@0: // XXX should report error michael@0: return false; michael@0: } michael@0: memcpy(*((jschar**)d), chars, alloc_len); michael@0: (*((jschar**)d))[count] = 0; michael@0: michael@0: return true; michael@0: } michael@0: default: michael@0: XPC_LOG_ERROR(("XPCConvert::JSStringWithSize2Native : unsupported type")); michael@0: return false; michael@0: } michael@0: } michael@0: