1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/src/nsDOMQS.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,256 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef nsDOMQS_h__ 1.11 +#define nsDOMQS_h__ 1.12 + 1.13 +#include "nsDOMClassInfoID.h" 1.14 +#include "nsGenericHTMLElement.h" 1.15 +#include "mozilla/dom/HTMLCanvasElement.h" 1.16 +#include "mozilla/dom/HTMLFormElement.h" 1.17 +#include "mozilla/dom/HTMLImageElement.h" 1.18 +#include "mozilla/dom/HTMLOptionElement.h" 1.19 +#include "HTMLOptGroupElement.h" 1.20 +#include "mozilla/dom/HTMLVideoElement.h" 1.21 +#include "nsHTMLDocument.h" 1.22 +#include "nsICSSDeclaration.h" 1.23 +#include "nsSVGElement.h" 1.24 +#include "mozilla/dom/Event.h" 1.25 +#include "mozilla/dom/UIEvent.h" 1.26 +#include "mozilla/dom/MouseEvent.h" 1.27 +#include "mozilla/dom/EventTargetBinding.h" 1.28 +#include "mozilla/dom/NodeBinding.h" 1.29 +#include "mozilla/dom/ElementBinding.h" 1.30 +#include "mozilla/dom/HTMLElementBinding.h" 1.31 +#include "mozilla/dom/DocumentBinding.h" 1.32 +#include "mozilla/dom/SVGElementBinding.h" 1.33 +#include "mozilla/dom/HTMLDocumentBinding.h" 1.34 +#include "XPCQuickStubs.h" 1.35 +#include "nsGlobalWindow.h" 1.36 + 1.37 +template<class T> 1.38 +struct ProtoIDAndDepth 1.39 +{ 1.40 + enum { 1.41 + PrototypeID = mozilla::dom::prototypes::id::_ID_Count, 1.42 + Depth = -1 1.43 + }; 1.44 +}; 1.45 + 1.46 +#define NEW_BINDING(_native, _id) \ 1.47 +template<> \ 1.48 +struct ProtoIDAndDepth<_native> \ 1.49 +{ \ 1.50 + enum { \ 1.51 + PrototypeID = mozilla::dom::prototypes::id::_id, \ 1.52 + Depth = mozilla::dom::PrototypeTraits< \ 1.53 + static_cast<mozilla::dom::prototypes::ID>(PrototypeID)>::Depth \ 1.54 + }; \ 1.55 +} 1.56 + 1.57 +NEW_BINDING(mozilla::dom::EventTarget, EventTarget); 1.58 +NEW_BINDING(nsINode, Node); 1.59 +NEW_BINDING(mozilla::dom::Element, Element); 1.60 +NEW_BINDING(nsGenericHTMLElement, HTMLElement); 1.61 +NEW_BINDING(nsIDocument, Document); 1.62 +NEW_BINDING(nsDocument, Document); 1.63 +NEW_BINDING(nsHTMLDocument, HTMLDocument); 1.64 +NEW_BINDING(nsSVGElement, SVGElement); 1.65 +NEW_BINDING(mozilla::dom::Event, Event); 1.66 +NEW_BINDING(mozilla::dom::UIEvent, UIEvent); 1.67 +NEW_BINDING(mozilla::dom::MouseEvent, MouseEvent); 1.68 +NEW_BINDING(nsGlobalWindow, Window); 1.69 + 1.70 +#define DEFINE_UNWRAP_CAST(_interface, _base, _bit) \ 1.71 +template <> \ 1.72 +MOZ_ALWAYS_INLINE bool \ 1.73 +xpc_qsUnwrapThis<_interface>(JSContext *cx, \ 1.74 + JS::HandleObject obj, \ 1.75 + _interface **ppThis, \ 1.76 + nsISupports **pThisRef, \ 1.77 + JS::MutableHandleValue pThisVal, \ 1.78 + bool failureFatal) \ 1.79 +{ \ 1.80 + nsresult rv; \ 1.81 + nsISupports *native = \ 1.82 + castNativeFromWrapper(cx, obj, _bit, \ 1.83 + ProtoIDAndDepth<_interface>::PrototypeID, \ 1.84 + ProtoIDAndDepth<_interface>::Depth, \ 1.85 + pThisRef, pThisVal, &rv); \ 1.86 + *ppThis = nullptr; /* avoids uninitialized warnings in callers */ \ 1.87 + if (failureFatal && !native) \ 1.88 + return xpc_qsThrow(cx, rv); \ 1.89 + *ppThis = static_cast<_interface*>(static_cast<_base*>(native)); \ 1.90 + return true; \ 1.91 +} \ 1.92 + \ 1.93 +template <> \ 1.94 +MOZ_ALWAYS_INLINE nsresult \ 1.95 +xpc_qsUnwrapArg<_interface>(JSContext *cx, \ 1.96 + JS::HandleValue v, \ 1.97 + _interface **ppArg, \ 1.98 + nsISupports **ppArgRef, \ 1.99 + JS::MutableHandleValue vp) \ 1.100 +{ \ 1.101 + nsresult rv; \ 1.102 + nsISupports *native = \ 1.103 + castNativeArgFromWrapper(cx, v, _bit, \ 1.104 + ProtoIDAndDepth<_interface>::PrototypeID, \ 1.105 + ProtoIDAndDepth<_interface>::Depth, \ 1.106 + ppArgRef, vp, &rv); \ 1.107 + if (NS_SUCCEEDED(rv)) \ 1.108 + *ppArg = static_cast<_interface*>(static_cast<_base*>(native)); \ 1.109 + return rv; \ 1.110 +} \ 1.111 + \ 1.112 +template <> \ 1.113 +inline nsresult \ 1.114 +xpc_qsUnwrapArg<_interface>(JSContext *cx, \ 1.115 + JS::HandleValue v, \ 1.116 + _interface **ppArg, \ 1.117 + _interface **ppArgRef, \ 1.118 + JS::MutableHandleValue vp) \ 1.119 +{ \ 1.120 + nsISupports* argRef = static_cast<_base*>(*ppArgRef); \ 1.121 + nsresult rv = xpc_qsUnwrapArg<_interface>(cx, v, ppArg, &argRef, vp); \ 1.122 + *ppArgRef = static_cast<_interface*>(static_cast<_base*>(argRef)); \ 1.123 + return rv; \ 1.124 +} \ 1.125 + \ 1.126 +namespace mozilla { \ 1.127 +namespace dom { \ 1.128 + \ 1.129 +template <> \ 1.130 +MOZ_ALWAYS_INLINE nsresult \ 1.131 +UnwrapArg<_interface>(JSContext *cx, \ 1.132 + JS::HandleValue v, \ 1.133 + _interface **ppArg, \ 1.134 + nsISupports **ppArgRef, \ 1.135 + JS::MutableHandleValue vp) \ 1.136 +{ \ 1.137 + return xpc_qsUnwrapArg<_interface>(cx, v, ppArg, ppArgRef, vp); \ 1.138 +} \ 1.139 + \ 1.140 +template <> \ 1.141 +inline nsresult \ 1.142 +UnwrapArg<_interface>(JSContext *cx, \ 1.143 + JS::HandleValue v, \ 1.144 + _interface **ppArg, \ 1.145 + _interface **ppArgRef, \ 1.146 + JS::MutableHandleValue vp) \ 1.147 +{ \ 1.148 + return xpc_qsUnwrapArg<_interface>(cx, v, ppArg, ppArgRef, vp); \ 1.149 +} \ 1.150 + \ 1.151 +} /* namespace dom */ \ 1.152 +} /* namespace mozilla */ 1.153 + 1.154 +#undef DOMCI_CASTABLE_INTERFACE 1.155 + 1.156 +#undef DOMCI_CASTABLE_INTERFACE 1.157 +#define DOMCI_CASTABLE_INTERFACE(_interface, _base, _bit, _extra) \ 1.158 + DEFINE_UNWRAP_CAST(_interface, _base, _bit) 1.159 + 1.160 +DOMCI_CASTABLE_INTERFACES(unused) 1.161 + 1.162 +#undef DOMCI_CASTABLE_INTERFACE 1.163 + 1.164 +inline nsresult 1.165 +xpc_qsUnwrapArg_HTMLElement(JSContext *cx, 1.166 + JS::HandleValue v, 1.167 + nsIAtom *aTag, 1.168 + nsIContent **ppArg, 1.169 + nsISupports **ppArgRef, 1.170 + JS::MutableHandleValue vp) 1.171 +{ 1.172 + nsGenericHTMLElement *elem; 1.173 + JS::RootedValue val(cx); 1.174 + nsresult rv = 1.175 + xpc_qsUnwrapArg<nsGenericHTMLElement>(cx, v, &elem, ppArgRef, &val); 1.176 + if (NS_SUCCEEDED(rv)) { 1.177 + if (elem->IsHTML(aTag)) { 1.178 + *ppArg = elem; 1.179 + vp.set(val); 1.180 + } else { 1.181 + rv = NS_ERROR_XPC_BAD_CONVERT_JS; 1.182 + } 1.183 + } 1.184 + return rv; 1.185 +} 1.186 + 1.187 +#define DEFINE_UNWRAP_CAST_HTML(_tag, _clazz) \ 1.188 +template <> \ 1.189 +inline nsresult \ 1.190 +xpc_qsUnwrapArg<_clazz>(JSContext *cx, \ 1.191 + JS::HandleValue v, \ 1.192 + _clazz **ppArg, \ 1.193 + nsISupports **ppArgRef, \ 1.194 + JS::MutableHandleValue vp) \ 1.195 +{ \ 1.196 + nsIContent *elem; \ 1.197 + nsresult rv = xpc_qsUnwrapArg_HTMLElement(cx, v, nsGkAtoms::_tag, &elem, \ 1.198 + ppArgRef, vp); \ 1.199 + if (NS_SUCCEEDED(rv)) \ 1.200 + *ppArg = static_cast<_clazz*>(elem); \ 1.201 + return rv; \ 1.202 +} \ 1.203 + \ 1.204 +template <> \ 1.205 +inline nsresult \ 1.206 +xpc_qsUnwrapArg<_clazz>(JSContext *cx, JS::HandleValue v, _clazz **ppArg, \ 1.207 + _clazz **ppArgRef, JS::MutableHandleValue vp) \ 1.208 +{ \ 1.209 + nsISupports* argRef = static_cast<nsIContent*>(*ppArgRef); \ 1.210 + nsresult rv = xpc_qsUnwrapArg<_clazz>(cx, v, ppArg, &argRef, vp); \ 1.211 + *ppArgRef = static_cast<_clazz*>(static_cast<nsIContent*>(argRef)); \ 1.212 + return rv; \ 1.213 +} \ 1.214 + \ 1.215 +namespace mozilla { \ 1.216 +namespace dom { \ 1.217 + \ 1.218 +template <> \ 1.219 +inline nsresult \ 1.220 +UnwrapArg<_clazz>(JSContext *cx, \ 1.221 + JS::HandleValue v, \ 1.222 + _clazz **ppArg, \ 1.223 + nsISupports **ppArgRef, \ 1.224 + JS::MutableHandleValue vp) \ 1.225 +{ \ 1.226 + return xpc_qsUnwrapArg<_clazz>(cx, v, ppArg, ppArgRef, vp); \ 1.227 +} \ 1.228 + \ 1.229 +template <> \ 1.230 +inline nsresult \ 1.231 +UnwrapArg<_clazz>(JSContext *cx, JS::HandleValue v, _clazz **ppArg, \ 1.232 + _clazz **ppArgRef, JS::MutableHandleValue vp) \ 1.233 +{ \ 1.234 + return xpc_qsUnwrapArg<_clazz>(cx, v, ppArg, ppArgRef, vp); \ 1.235 +} \ 1.236 + \ 1.237 +} /* namespace dom */ \ 1.238 +} /* namespace mozilla */ 1.239 + 1.240 +DEFINE_UNWRAP_CAST_HTML(canvas, mozilla::dom::HTMLCanvasElement) 1.241 +DEFINE_UNWRAP_CAST_HTML(form, mozilla::dom::HTMLFormElement) 1.242 +DEFINE_UNWRAP_CAST_HTML(img, mozilla::dom::HTMLImageElement) 1.243 +DEFINE_UNWRAP_CAST_HTML(optgroup, mozilla::dom::HTMLOptGroupElement) 1.244 +DEFINE_UNWRAP_CAST_HTML(option, mozilla::dom::HTMLOptionElement) 1.245 +DEFINE_UNWRAP_CAST_HTML(video, mozilla::dom::HTMLVideoElement) 1.246 + 1.247 +inline nsISupports* 1.248 +ToSupports(nsContentList *p) 1.249 +{ 1.250 + return static_cast<nsINodeList*>(p); 1.251 +} 1.252 + 1.253 +inline nsISupports* 1.254 +ToCanonicalSupports(nsContentList *p) 1.255 +{ 1.256 + return static_cast<nsINodeList*>(p); 1.257 +} 1.258 + 1.259 +#endif /* nsDOMQS_h__ */