diff options
Diffstat (limited to 'js/xpconnect/public')
-rw-r--r-- | js/xpconnect/public/moz.build | 13 | ||||
-rw-r--r-- | js/xpconnect/public/nsAXPCNativeCallContext.h | 32 | ||||
-rw-r--r-- | js/xpconnect/public/nsTArrayHelpers.h | 92 | ||||
-rw-r--r-- | js/xpconnect/public/xpc_make_class.h | 175 | ||||
-rw-r--r-- | js/xpconnect/public/xpc_map_end.h | 204 |
5 files changed, 516 insertions, 0 deletions
diff --git a/js/xpconnect/public/moz.build b/js/xpconnect/public/moz.build new file mode 100644 index 000000000..b6650e395 --- /dev/null +++ b/js/xpconnect/public/moz.build @@ -0,0 +1,13 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +EXPORTS += [ + 'nsAXPCNativeCallContext.h', + 'nsTArrayHelpers.h', + 'xpc_make_class.h', + 'xpc_map_end.h', +] + diff --git a/js/xpconnect/public/nsAXPCNativeCallContext.h b/js/xpconnect/public/nsAXPCNativeCallContext.h new file mode 100644 index 000000000..aeafe7ebe --- /dev/null +++ b/js/xpconnect/public/nsAXPCNativeCallContext.h @@ -0,0 +1,32 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ +/* 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/. */ + +#ifndef nsAXPCNativeCallContext_h__ +#define nsAXPCNativeCallContext_h__ + +/** +* A native call context is allocated on the stack when XPConnect calls a +* native method. Holding a pointer to this object beyond the currently +* executing stack frame is not permitted. +*/ +class nsAXPCNativeCallContext +{ +public: + NS_IMETHOD GetCallee(nsISupports** aResult) = 0; + NS_IMETHOD GetCalleeMethodIndex(uint16_t* aResult) = 0; + NS_IMETHOD GetJSContext(JSContext** aResult) = 0; + NS_IMETHOD GetArgc(uint32_t* aResult) = 0; + NS_IMETHOD GetArgvPtr(JS::Value** aResult) = 0; + + // Methods added since mozilla 0.6.... + + NS_IMETHOD GetCalleeInterface(nsIInterfaceInfo** aResult) = 0; + NS_IMETHOD GetCalleeClassInfo(nsIClassInfo** aResult) = 0; + + NS_IMETHOD GetPreviousCallContext(nsAXPCNativeCallContext** aResult) = 0; +}; + +#endif diff --git a/js/xpconnect/public/nsTArrayHelpers.h b/js/xpconnect/public/nsTArrayHelpers.h new file mode 100644 index 000000000..aa0ce8198 --- /dev/null +++ b/js/xpconnect/public/nsTArrayHelpers.h @@ -0,0 +1,92 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef __NSTARRAYHELPERS_H__ +#define __NSTARRAYHELPERS_H__ + +#include "jsapi.h" +#include "nsContentUtils.h" +#include "nsTArray.h" + +template <class T> +inline nsresult +nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray, + JS::MutableHandle<JSObject*> aResultArray) +{ + MOZ_ASSERT(aCx); + + JS::Rooted<JSObject*> arrayObj(aCx, + JS_NewArrayObject(aCx, aSourceArray.Length())); + if (!arrayObj) { + NS_WARNING("JS_NewArrayObject failed!"); + return NS_ERROR_OUT_OF_MEMORY; + } + + for (uint32_t index = 0; index < aSourceArray.Length(); index++) { + nsCOMPtr<nsISupports> obj; + nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj)); + NS_ENSURE_SUCCESS(rv, rv); + + JS::RootedValue wrappedVal(aCx); + rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal); + NS_ENSURE_SUCCESS(rv, rv); + + if (!JS_DefineElement(aCx, arrayObj, index, wrappedVal, JSPROP_ENUMERATE)) { + NS_WARNING("JS_DefineElement failed!"); + return NS_ERROR_FAILURE; + } + } + + if (!JS_FreezeObject(aCx, arrayObj)) { + NS_WARNING("JS_FreezeObject failed!"); + return NS_ERROR_FAILURE; + } + + aResultArray.set(arrayObj); + return NS_OK; +} + +template <> +inline nsresult +nsTArrayToJSArray<nsString>(JSContext* aCx, + const nsTArray<nsString>& aSourceArray, + JS::MutableHandle<JSObject*> aResultArray) +{ + MOZ_ASSERT(aCx); + + JS::Rooted<JSObject*> arrayObj(aCx, + JS_NewArrayObject(aCx, aSourceArray.Length())); + if (!arrayObj) { + NS_WARNING("JS_NewArrayObject failed!"); + return NS_ERROR_OUT_OF_MEMORY; + } + + JS::Rooted<JSString*> s(aCx); + for (uint32_t index = 0; index < aSourceArray.Length(); index++) { + s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(), + aSourceArray[index].Length()); + + if(!s) { + NS_WARNING("Memory allocation error!"); + return NS_ERROR_OUT_OF_MEMORY; + } + + if (!JS_DefineElement(aCx, arrayObj, index, s, JSPROP_ENUMERATE)) { + NS_WARNING("JS_DefineElement failed!"); + return NS_ERROR_FAILURE; + } + } + + if (!JS_FreezeObject(aCx, arrayObj)) { + NS_WARNING("JS_FreezeObject failed!"); + return NS_ERROR_FAILURE; + } + + aResultArray.set(arrayObj); + return NS_OK; +} + +#endif /* __NSTARRAYHELPERS_H__ */ diff --git a/js/xpconnect/public/xpc_make_class.h b/js/xpconnect/public/xpc_make_class.h new file mode 100644 index 000000000..f612d3056 --- /dev/null +++ b/js/xpconnect/public/xpc_make_class.h @@ -0,0 +1,175 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ +/* 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/. */ + +#ifndef xpc_make_class_h +#define xpc_make_class_h + +// This file should be used to create js::Class instances for nsIXPCScriptable +// instances. This includes any file that uses xpc_map_end.h. + +#include "xpcpublic.h" +#include "mozilla/dom/DOMJSClass.h" + +bool +XPC_WN_MaybeResolvingPropertyStub(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, JS::HandleValue v); +bool +XPC_WN_CannotModifyPropertyStub(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, JS::HandleValue v); + +bool +XPC_WN_MaybeResolvingDeletePropertyStub(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, + JS::ObjectOpResult& result); +bool +XPC_WN_CantDeletePropertyStub(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, JS::ObjectOpResult& result); + +bool +XPC_WN_Helper_GetProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, + JS::MutableHandleValue vp); + +bool +XPC_WN_Helper_SetProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, + JS::MutableHandleValue vp, + JS::ObjectOpResult& result); +bool +XPC_WN_MaybeResolvingSetPropertyStub(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, JS::MutableHandleValue vp, + JS::ObjectOpResult& result); +bool +XPC_WN_CannotModifySetPropertyStub(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, JS::MutableHandleValue vp, + JS::ObjectOpResult& result); + +bool +XPC_WN_Helper_Enumerate(JSContext* cx, JS::HandleObject obj); +bool +XPC_WN_Shared_Enumerate(JSContext* cx, JS::HandleObject obj); + +bool +XPC_WN_Helper_Resolve(JSContext* cx, JS::HandleObject obj, JS::HandleId id, + bool* resolvedp); + +void +XPC_WN_Helper_Finalize(js::FreeOp* fop, JSObject* obj); +void +XPC_WN_NoHelper_Finalize(js::FreeOp* fop, JSObject* obj); + +bool +XPC_WN_Helper_Call(JSContext* cx, unsigned argc, JS::Value* vp); + +bool +XPC_WN_Helper_HasInstance(JSContext* cx, JS::HandleObject obj, + JS::MutableHandleValue valp, bool* bp); + +bool +XPC_WN_Helper_Construct(JSContext* cx, unsigned argc, JS::Value* vp); + +void +XPCWrappedNative_Trace(JSTracer* trc, JSObject* obj); + +extern const js::ClassExtension XPC_WN_JSClassExtension; + +extern const js::ObjectOps XPC_WN_ObjectOpsWithEnumerate; + +#define XPC_MAKE_CLASS_OPS(_flags) { \ + /* addProperty */ \ + ((_flags) & nsIXPCScriptable::USE_JSSTUB_FOR_ADDPROPERTY) \ + ? nullptr \ + : ((_flags) & nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE) \ + ? XPC_WN_MaybeResolvingPropertyStub \ + : XPC_WN_CannotModifyPropertyStub, \ + \ + /* delProperty */ \ + ((_flags) & nsIXPCScriptable::USE_JSSTUB_FOR_DELPROPERTY) \ + ? nullptr \ + : ((_flags) & nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE) \ + ? XPC_WN_MaybeResolvingDeletePropertyStub \ + : XPC_WN_CantDeletePropertyStub, \ + \ + /* getProperty */ \ + ((_flags) & nsIXPCScriptable::WANT_GETPROPERTY) \ + ? XPC_WN_Helper_GetProperty \ + : nullptr, \ + \ + /* setProperty */ \ + ((_flags) & nsIXPCScriptable::WANT_SETPROPERTY) \ + ? XPC_WN_Helper_SetProperty \ + : ((_flags) & nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY) \ + ? nullptr \ + : ((_flags) & nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE) \ + ? XPC_WN_MaybeResolvingSetPropertyStub \ + : XPC_WN_CannotModifySetPropertyStub, \ + \ + /* enumerate */ \ + ((_flags) & nsIXPCScriptable::WANT_NEWENUMERATE) \ + ? nullptr /* We will use oOps->enumerate set below in this case */ \ + : ((_flags) & nsIXPCScriptable::WANT_ENUMERATE) \ + ? XPC_WN_Helper_Enumerate \ + : XPC_WN_Shared_Enumerate, \ + \ + /* resolve */ \ + /* We have to figure out resolve strategy at call time */ \ + XPC_WN_Helper_Resolve, \ + \ + /* mayResolve */ \ + nullptr, \ + \ + /* finalize */ \ + ((_flags) & nsIXPCScriptable::WANT_FINALIZE) \ + ? XPC_WN_Helper_Finalize \ + : XPC_WN_NoHelper_Finalize, \ + \ + /* call */ \ + ((_flags) & nsIXPCScriptable::WANT_CALL) \ + ? XPC_WN_Helper_Call \ + : nullptr, \ + \ + /* hasInstance */ \ + ((_flags) & nsIXPCScriptable::WANT_HASINSTANCE) \ + ? XPC_WN_Helper_HasInstance \ + : nullptr, \ + \ + /* construct */ \ + ((_flags) & nsIXPCScriptable::WANT_CONSTRUCT) \ + ? XPC_WN_Helper_Construct \ + : nullptr, \ + \ + /* trace */ \ + ((_flags) & nsIXPCScriptable::IS_GLOBAL_OBJECT) \ + ? JS_GlobalObjectTraceHook \ + : XPCWrappedNative_Trace, \ +} + +#define XPC_MAKE_CLASS(_name, _flags, _classOps) { \ + /* name */ \ + _name, \ + \ + /* flags */ \ + XPC_WRAPPER_FLAGS | \ + JSCLASS_PRIVATE_IS_NSISUPPORTS | \ + JSCLASS_IS_WRAPPED_NATIVE | \ + (((_flags) & nsIXPCScriptable::IS_GLOBAL_OBJECT) \ + ? XPCONNECT_GLOBAL_FLAGS \ + : 0), \ + \ + /* cOps */ \ + _classOps, \ + \ + /* spec */ \ + nullptr, \ + \ + /* ext */ \ + &XPC_WN_JSClassExtension, \ + \ + /* oOps */ \ + ((_flags) & nsIXPCScriptable::WANT_NEWENUMERATE) \ + ? &XPC_WN_ObjectOpsWithEnumerate \ + : nullptr, \ +} + +#endif diff --git a/js/xpconnect/public/xpc_map_end.h b/js/xpconnect/public/xpc_map_end.h new file mode 100644 index 000000000..dfb7b10d4 --- /dev/null +++ b/js/xpconnect/public/xpc_map_end.h @@ -0,0 +1,204 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ +/* 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/. */ + +// If you include this file you must also include xpc_make_class.h at the top +// of the file doing the including. + +#ifndef XPC_MAP_CLASSNAME +#error "Must #define XPC_MAP_CLASSNAME before #including xpc_map_end.h" +#endif + +#ifndef XPC_MAP_QUOTED_CLASSNAME +#error "Must #define XPC_MAP_QUOTED_CLASSNAME before #including xpc_map_end.h" +#endif + +#include "js/Id.h" + +/**************************************************************/ + +NS_IMETHODIMP XPC_MAP_CLASSNAME::GetClassName(char * *aClassName) +{ + static const char sName[] = XPC_MAP_QUOTED_CLASSNAME; + *aClassName = (char*) nsMemory::Clone(sName, sizeof(sName)); + return NS_OK; +} + +/**************************************************************/ + +// virtual +uint32_t +XPC_MAP_CLASSNAME::GetScriptableFlags() +{ + return +#ifdef XPC_MAP_WANT_PRECREATE + nsIXPCScriptable::WANT_PRECREATE | +#endif +#ifdef XPC_MAP_WANT_ADDPROPERTY + nsIXPCScriptable::WANT_ADDPROPERTY | +#endif +#ifdef XPC_MAP_WANT_GETPROPERTY + nsIXPCScriptable::WANT_GETPROPERTY | +#endif +#ifdef XPC_MAP_WANT_SETPROPERTY + nsIXPCScriptable::WANT_SETPROPERTY | +#endif +#ifdef XPC_MAP_WANT_ENUMERATE + nsIXPCScriptable::WANT_ENUMERATE | +#endif +#ifdef XPC_MAP_WANT_NEWENUMERATE + nsIXPCScriptable::WANT_NEWENUMERATE | +#endif +#ifdef XPC_MAP_WANT_RESOLVE + nsIXPCScriptable::WANT_RESOLVE | +#endif +#ifdef XPC_MAP_WANT_FINALIZE + nsIXPCScriptable::WANT_FINALIZE | +#endif +#ifdef XPC_MAP_WANT_CALL + nsIXPCScriptable::WANT_CALL | +#endif +#ifdef XPC_MAP_WANT_CONSTRUCT + nsIXPCScriptable::WANT_CONSTRUCT | +#endif +#ifdef XPC_MAP_WANT_HASINSTANCE + nsIXPCScriptable::WANT_HASINSTANCE | +#endif +#ifdef XPC_MAP_FLAGS + XPC_MAP_FLAGS | +#endif + 0; +} + +// virtual +const js::Class* +XPC_MAP_CLASSNAME::GetClass() +{ + static const js::ClassOps classOps = + XPC_MAKE_CLASS_OPS(GetScriptableFlags()); + static const js::Class klass = + XPC_MAKE_CLASS(XPC_MAP_QUOTED_CLASSNAME, GetScriptableFlags(), + &classOps); + return &klass; +} + +/**************************************************************/ + +#ifndef XPC_MAP_WANT_PRECREATE +NS_IMETHODIMP XPC_MAP_CLASSNAME::PreCreate(nsISupports* nativeObj, JSContext * cx, JSObject * globalObj, JSObject * *parentObj) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_ADDPROPERTY +NS_IMETHODIMP XPC_MAP_CLASSNAME::AddProperty(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, jsid id, JS::HandleValue val, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_GETPROPERTY +NS_IMETHODIMP XPC_MAP_CLASSNAME::GetProperty(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, jsid id, JS::Value * vp, bool* _retval) + {NS_WARNING("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_SETPROPERTY +NS_IMETHODIMP XPC_MAP_CLASSNAME::SetProperty(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, jsid id, JS::Value * vp, bool* _retval) + {NS_WARNING("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_NEWENUMERATE +NS_IMETHODIMP XPC_MAP_CLASSNAME::NewEnumerate(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, JS::AutoIdVector& properties, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_ENUMERATE +NS_IMETHODIMP XPC_MAP_CLASSNAME::Enumerate(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_RESOLVE +NS_IMETHODIMP XPC_MAP_CLASSNAME::Resolve(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, jsid id, bool* resolvedp, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_FINALIZE +NS_IMETHODIMP XPC_MAP_CLASSNAME::Finalize(nsIXPConnectWrappedNative* wrapper, JSFreeOp * fop, JSObject * obj) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_CALL +NS_IMETHODIMP XPC_MAP_CLASSNAME::Call(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, const JS::CallArgs& args, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_CONSTRUCT +NS_IMETHODIMP XPC_MAP_CLASSNAME::Construct(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, const JS::CallArgs& args, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_HASINSTANCE +NS_IMETHODIMP XPC_MAP_CLASSNAME::HasInstance(nsIXPConnectWrappedNative* wrapper, JSContext * cx, JSObject * obj, JS::HandleValue val, bool* bp, bool* _retval) + {NS_ERROR("never called"); return NS_ERROR_NOT_IMPLEMENTED;} +#endif + +#ifndef XPC_MAP_WANT_POST_CREATE_PROTOTYPE +NS_IMETHODIMP XPC_MAP_CLASSNAME::PostCreatePrototype(JSContext* cx, JSObject* proto) + {return NS_OK;} +#endif + +/**************************************************************/ + +#undef XPC_MAP_CLASSNAME +#undef XPC_MAP_QUOTED_CLASSNAME + +#ifdef XPC_MAP_WANT_PRECREATE +#undef XPC_MAP_WANT_PRECREATE +#endif + +#ifdef XPC_MAP_WANT_ADDPROPERTY +#undef XPC_MAP_WANT_ADDPROPERTY +#endif + +#ifdef XPC_MAP_WANT_GETPROPERTY +#undef XPC_MAP_WANT_GETPROPERTY +#endif + +#ifdef XPC_MAP_WANT_SETPROPERTY +#undef XPC_MAP_WANT_SETPROPERTY +#endif + +#ifdef XPC_MAP_WANT_ENUMERATE +#undef XPC_MAP_WANT_ENUMERATE +#endif + +#ifdef XPC_MAP_WANT_NEWENUMERATE +#undef XPC_MAP_WANT_NEWENUMERATE +#endif + +#ifdef XPC_MAP_WANT_RESOLVE +#undef XPC_MAP_WANT_RESOLVE +#endif + +#ifdef XPC_MAP_WANT_FINALIZE +#undef XPC_MAP_WANT_FINALIZE +#endif + +#ifdef XPC_MAP_WANT_CALL +#undef XPC_MAP_WANT_CALL +#endif + +#ifdef XPC_MAP_WANT_CONSTRUCT +#undef XPC_MAP_WANT_CONSTRUCT +#endif + +#ifdef XPC_MAP_WANT_HASINSTANCE +#undef XPC_MAP_WANT_HASINSTANCE +#endif + +#ifdef XPC_MAP_WANT_POST_CREATE_PROTOTYPE +#undef XPC_MAP_WANT_POST_CREATE_PROTOTYPE +#endif + +#ifdef XPC_MAP_FLAGS +#undef XPC_MAP_FLAGS +#endif |