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