Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef nsDOMQS_h__ |
michael@0 | 8 | #define nsDOMQS_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsDOMClassInfoID.h" |
michael@0 | 11 | #include "nsGenericHTMLElement.h" |
michael@0 | 12 | #include "mozilla/dom/HTMLCanvasElement.h" |
michael@0 | 13 | #include "mozilla/dom/HTMLFormElement.h" |
michael@0 | 14 | #include "mozilla/dom/HTMLImageElement.h" |
michael@0 | 15 | #include "mozilla/dom/HTMLOptionElement.h" |
michael@0 | 16 | #include "HTMLOptGroupElement.h" |
michael@0 | 17 | #include "mozilla/dom/HTMLVideoElement.h" |
michael@0 | 18 | #include "nsHTMLDocument.h" |
michael@0 | 19 | #include "nsICSSDeclaration.h" |
michael@0 | 20 | #include "nsSVGElement.h" |
michael@0 | 21 | #include "mozilla/dom/Event.h" |
michael@0 | 22 | #include "mozilla/dom/UIEvent.h" |
michael@0 | 23 | #include "mozilla/dom/MouseEvent.h" |
michael@0 | 24 | #include "mozilla/dom/EventTargetBinding.h" |
michael@0 | 25 | #include "mozilla/dom/NodeBinding.h" |
michael@0 | 26 | #include "mozilla/dom/ElementBinding.h" |
michael@0 | 27 | #include "mozilla/dom/HTMLElementBinding.h" |
michael@0 | 28 | #include "mozilla/dom/DocumentBinding.h" |
michael@0 | 29 | #include "mozilla/dom/SVGElementBinding.h" |
michael@0 | 30 | #include "mozilla/dom/HTMLDocumentBinding.h" |
michael@0 | 31 | #include "XPCQuickStubs.h" |
michael@0 | 32 | #include "nsGlobalWindow.h" |
michael@0 | 33 | |
michael@0 | 34 | template<class T> |
michael@0 | 35 | struct ProtoIDAndDepth |
michael@0 | 36 | { |
michael@0 | 37 | enum { |
michael@0 | 38 | PrototypeID = mozilla::dom::prototypes::id::_ID_Count, |
michael@0 | 39 | Depth = -1 |
michael@0 | 40 | }; |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | #define NEW_BINDING(_native, _id) \ |
michael@0 | 44 | template<> \ |
michael@0 | 45 | struct ProtoIDAndDepth<_native> \ |
michael@0 | 46 | { \ |
michael@0 | 47 | enum { \ |
michael@0 | 48 | PrototypeID = mozilla::dom::prototypes::id::_id, \ |
michael@0 | 49 | Depth = mozilla::dom::PrototypeTraits< \ |
michael@0 | 50 | static_cast<mozilla::dom::prototypes::ID>(PrototypeID)>::Depth \ |
michael@0 | 51 | }; \ |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | NEW_BINDING(mozilla::dom::EventTarget, EventTarget); |
michael@0 | 55 | NEW_BINDING(nsINode, Node); |
michael@0 | 56 | NEW_BINDING(mozilla::dom::Element, Element); |
michael@0 | 57 | NEW_BINDING(nsGenericHTMLElement, HTMLElement); |
michael@0 | 58 | NEW_BINDING(nsIDocument, Document); |
michael@0 | 59 | NEW_BINDING(nsDocument, Document); |
michael@0 | 60 | NEW_BINDING(nsHTMLDocument, HTMLDocument); |
michael@0 | 61 | NEW_BINDING(nsSVGElement, SVGElement); |
michael@0 | 62 | NEW_BINDING(mozilla::dom::Event, Event); |
michael@0 | 63 | NEW_BINDING(mozilla::dom::UIEvent, UIEvent); |
michael@0 | 64 | NEW_BINDING(mozilla::dom::MouseEvent, MouseEvent); |
michael@0 | 65 | NEW_BINDING(nsGlobalWindow, Window); |
michael@0 | 66 | |
michael@0 | 67 | #define DEFINE_UNWRAP_CAST(_interface, _base, _bit) \ |
michael@0 | 68 | template <> \ |
michael@0 | 69 | MOZ_ALWAYS_INLINE bool \ |
michael@0 | 70 | xpc_qsUnwrapThis<_interface>(JSContext *cx, \ |
michael@0 | 71 | JS::HandleObject obj, \ |
michael@0 | 72 | _interface **ppThis, \ |
michael@0 | 73 | nsISupports **pThisRef, \ |
michael@0 | 74 | JS::MutableHandleValue pThisVal, \ |
michael@0 | 75 | bool failureFatal) \ |
michael@0 | 76 | { \ |
michael@0 | 77 | nsresult rv; \ |
michael@0 | 78 | nsISupports *native = \ |
michael@0 | 79 | castNativeFromWrapper(cx, obj, _bit, \ |
michael@0 | 80 | ProtoIDAndDepth<_interface>::PrototypeID, \ |
michael@0 | 81 | ProtoIDAndDepth<_interface>::Depth, \ |
michael@0 | 82 | pThisRef, pThisVal, &rv); \ |
michael@0 | 83 | *ppThis = nullptr; /* avoids uninitialized warnings in callers */ \ |
michael@0 | 84 | if (failureFatal && !native) \ |
michael@0 | 85 | return xpc_qsThrow(cx, rv); \ |
michael@0 | 86 | *ppThis = static_cast<_interface*>(static_cast<_base*>(native)); \ |
michael@0 | 87 | return true; \ |
michael@0 | 88 | } \ |
michael@0 | 89 | \ |
michael@0 | 90 | template <> \ |
michael@0 | 91 | MOZ_ALWAYS_INLINE nsresult \ |
michael@0 | 92 | xpc_qsUnwrapArg<_interface>(JSContext *cx, \ |
michael@0 | 93 | JS::HandleValue v, \ |
michael@0 | 94 | _interface **ppArg, \ |
michael@0 | 95 | nsISupports **ppArgRef, \ |
michael@0 | 96 | JS::MutableHandleValue vp) \ |
michael@0 | 97 | { \ |
michael@0 | 98 | nsresult rv; \ |
michael@0 | 99 | nsISupports *native = \ |
michael@0 | 100 | castNativeArgFromWrapper(cx, v, _bit, \ |
michael@0 | 101 | ProtoIDAndDepth<_interface>::PrototypeID, \ |
michael@0 | 102 | ProtoIDAndDepth<_interface>::Depth, \ |
michael@0 | 103 | ppArgRef, vp, &rv); \ |
michael@0 | 104 | if (NS_SUCCEEDED(rv)) \ |
michael@0 | 105 | *ppArg = static_cast<_interface*>(static_cast<_base*>(native)); \ |
michael@0 | 106 | return rv; \ |
michael@0 | 107 | } \ |
michael@0 | 108 | \ |
michael@0 | 109 | template <> \ |
michael@0 | 110 | inline nsresult \ |
michael@0 | 111 | xpc_qsUnwrapArg<_interface>(JSContext *cx, \ |
michael@0 | 112 | JS::HandleValue v, \ |
michael@0 | 113 | _interface **ppArg, \ |
michael@0 | 114 | _interface **ppArgRef, \ |
michael@0 | 115 | JS::MutableHandleValue vp) \ |
michael@0 | 116 | { \ |
michael@0 | 117 | nsISupports* argRef = static_cast<_base*>(*ppArgRef); \ |
michael@0 | 118 | nsresult rv = xpc_qsUnwrapArg<_interface>(cx, v, ppArg, &argRef, vp); \ |
michael@0 | 119 | *ppArgRef = static_cast<_interface*>(static_cast<_base*>(argRef)); \ |
michael@0 | 120 | return rv; \ |
michael@0 | 121 | } \ |
michael@0 | 122 | \ |
michael@0 | 123 | namespace mozilla { \ |
michael@0 | 124 | namespace dom { \ |
michael@0 | 125 | \ |
michael@0 | 126 | template <> \ |
michael@0 | 127 | MOZ_ALWAYS_INLINE nsresult \ |
michael@0 | 128 | UnwrapArg<_interface>(JSContext *cx, \ |
michael@0 | 129 | JS::HandleValue v, \ |
michael@0 | 130 | _interface **ppArg, \ |
michael@0 | 131 | nsISupports **ppArgRef, \ |
michael@0 | 132 | JS::MutableHandleValue vp) \ |
michael@0 | 133 | { \ |
michael@0 | 134 | return xpc_qsUnwrapArg<_interface>(cx, v, ppArg, ppArgRef, vp); \ |
michael@0 | 135 | } \ |
michael@0 | 136 | \ |
michael@0 | 137 | template <> \ |
michael@0 | 138 | inline nsresult \ |
michael@0 | 139 | UnwrapArg<_interface>(JSContext *cx, \ |
michael@0 | 140 | JS::HandleValue v, \ |
michael@0 | 141 | _interface **ppArg, \ |
michael@0 | 142 | _interface **ppArgRef, \ |
michael@0 | 143 | JS::MutableHandleValue vp) \ |
michael@0 | 144 | { \ |
michael@0 | 145 | return xpc_qsUnwrapArg<_interface>(cx, v, ppArg, ppArgRef, vp); \ |
michael@0 | 146 | } \ |
michael@0 | 147 | \ |
michael@0 | 148 | } /* namespace dom */ \ |
michael@0 | 149 | } /* namespace mozilla */ |
michael@0 | 150 | |
michael@0 | 151 | #undef DOMCI_CASTABLE_INTERFACE |
michael@0 | 152 | |
michael@0 | 153 | #undef DOMCI_CASTABLE_INTERFACE |
michael@0 | 154 | #define DOMCI_CASTABLE_INTERFACE(_interface, _base, _bit, _extra) \ |
michael@0 | 155 | DEFINE_UNWRAP_CAST(_interface, _base, _bit) |
michael@0 | 156 | |
michael@0 | 157 | DOMCI_CASTABLE_INTERFACES(unused) |
michael@0 | 158 | |
michael@0 | 159 | #undef DOMCI_CASTABLE_INTERFACE |
michael@0 | 160 | |
michael@0 | 161 | inline nsresult |
michael@0 | 162 | xpc_qsUnwrapArg_HTMLElement(JSContext *cx, |
michael@0 | 163 | JS::HandleValue v, |
michael@0 | 164 | nsIAtom *aTag, |
michael@0 | 165 | nsIContent **ppArg, |
michael@0 | 166 | nsISupports **ppArgRef, |
michael@0 | 167 | JS::MutableHandleValue vp) |
michael@0 | 168 | { |
michael@0 | 169 | nsGenericHTMLElement *elem; |
michael@0 | 170 | JS::RootedValue val(cx); |
michael@0 | 171 | nsresult rv = |
michael@0 | 172 | xpc_qsUnwrapArg<nsGenericHTMLElement>(cx, v, &elem, ppArgRef, &val); |
michael@0 | 173 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 174 | if (elem->IsHTML(aTag)) { |
michael@0 | 175 | *ppArg = elem; |
michael@0 | 176 | vp.set(val); |
michael@0 | 177 | } else { |
michael@0 | 178 | rv = NS_ERROR_XPC_BAD_CONVERT_JS; |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | return rv; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | #define DEFINE_UNWRAP_CAST_HTML(_tag, _clazz) \ |
michael@0 | 185 | template <> \ |
michael@0 | 186 | inline nsresult \ |
michael@0 | 187 | xpc_qsUnwrapArg<_clazz>(JSContext *cx, \ |
michael@0 | 188 | JS::HandleValue v, \ |
michael@0 | 189 | _clazz **ppArg, \ |
michael@0 | 190 | nsISupports **ppArgRef, \ |
michael@0 | 191 | JS::MutableHandleValue vp) \ |
michael@0 | 192 | { \ |
michael@0 | 193 | nsIContent *elem; \ |
michael@0 | 194 | nsresult rv = xpc_qsUnwrapArg_HTMLElement(cx, v, nsGkAtoms::_tag, &elem, \ |
michael@0 | 195 | ppArgRef, vp); \ |
michael@0 | 196 | if (NS_SUCCEEDED(rv)) \ |
michael@0 | 197 | *ppArg = static_cast<_clazz*>(elem); \ |
michael@0 | 198 | return rv; \ |
michael@0 | 199 | } \ |
michael@0 | 200 | \ |
michael@0 | 201 | template <> \ |
michael@0 | 202 | inline nsresult \ |
michael@0 | 203 | xpc_qsUnwrapArg<_clazz>(JSContext *cx, JS::HandleValue v, _clazz **ppArg, \ |
michael@0 | 204 | _clazz **ppArgRef, JS::MutableHandleValue vp) \ |
michael@0 | 205 | { \ |
michael@0 | 206 | nsISupports* argRef = static_cast<nsIContent*>(*ppArgRef); \ |
michael@0 | 207 | nsresult rv = xpc_qsUnwrapArg<_clazz>(cx, v, ppArg, &argRef, vp); \ |
michael@0 | 208 | *ppArgRef = static_cast<_clazz*>(static_cast<nsIContent*>(argRef)); \ |
michael@0 | 209 | return rv; \ |
michael@0 | 210 | } \ |
michael@0 | 211 | \ |
michael@0 | 212 | namespace mozilla { \ |
michael@0 | 213 | namespace dom { \ |
michael@0 | 214 | \ |
michael@0 | 215 | template <> \ |
michael@0 | 216 | inline nsresult \ |
michael@0 | 217 | UnwrapArg<_clazz>(JSContext *cx, \ |
michael@0 | 218 | JS::HandleValue v, \ |
michael@0 | 219 | _clazz **ppArg, \ |
michael@0 | 220 | nsISupports **ppArgRef, \ |
michael@0 | 221 | JS::MutableHandleValue vp) \ |
michael@0 | 222 | { \ |
michael@0 | 223 | return xpc_qsUnwrapArg<_clazz>(cx, v, ppArg, ppArgRef, vp); \ |
michael@0 | 224 | } \ |
michael@0 | 225 | \ |
michael@0 | 226 | template <> \ |
michael@0 | 227 | inline nsresult \ |
michael@0 | 228 | UnwrapArg<_clazz>(JSContext *cx, JS::HandleValue v, _clazz **ppArg, \ |
michael@0 | 229 | _clazz **ppArgRef, JS::MutableHandleValue vp) \ |
michael@0 | 230 | { \ |
michael@0 | 231 | return xpc_qsUnwrapArg<_clazz>(cx, v, ppArg, ppArgRef, vp); \ |
michael@0 | 232 | } \ |
michael@0 | 233 | \ |
michael@0 | 234 | } /* namespace dom */ \ |
michael@0 | 235 | } /* namespace mozilla */ |
michael@0 | 236 | |
michael@0 | 237 | DEFINE_UNWRAP_CAST_HTML(canvas, mozilla::dom::HTMLCanvasElement) |
michael@0 | 238 | DEFINE_UNWRAP_CAST_HTML(form, mozilla::dom::HTMLFormElement) |
michael@0 | 239 | DEFINE_UNWRAP_CAST_HTML(img, mozilla::dom::HTMLImageElement) |
michael@0 | 240 | DEFINE_UNWRAP_CAST_HTML(optgroup, mozilla::dom::HTMLOptGroupElement) |
michael@0 | 241 | DEFINE_UNWRAP_CAST_HTML(option, mozilla::dom::HTMLOptionElement) |
michael@0 | 242 | DEFINE_UNWRAP_CAST_HTML(video, mozilla::dom::HTMLVideoElement) |
michael@0 | 243 | |
michael@0 | 244 | inline nsISupports* |
michael@0 | 245 | ToSupports(nsContentList *p) |
michael@0 | 246 | { |
michael@0 | 247 | return static_cast<nsINodeList*>(p); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | inline nsISupports* |
michael@0 | 251 | ToCanonicalSupports(nsContentList *p) |
michael@0 | 252 | { |
michael@0 | 253 | return static_cast<nsINodeList*>(p); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | #endif /* nsDOMQS_h__ */ |