1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is XPConnect code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #ifndef nsDOMQS_h__
38 : #define nsDOMQS_h__
39 :
40 : #include "nsDOMClassInfoID.h"
41 :
42 : #define DEFINE_UNWRAP_CAST(_interface, _base, _bit) \
43 : template <> \
44 : inline JSBool \
45 : xpc_qsUnwrapThis<_interface>(JSContext *cx, \
46 : JSObject *obj, \
47 : JSObject *callee, \
48 : _interface **ppThis, \
49 : nsISupports **pThisRef, \
50 : jsval *pThisVal, \
51 : XPCLazyCallContext *lccx, \
52 : bool failureFatal) \
53 : { \
54 : nsresult rv; \
55 : nsISupports *native = castNativeFromWrapper(cx, obj, callee, _bit, \
56 : pThisRef, pThisVal, lccx, \
57 : &rv); \
58 : *ppThis = NULL; /* avoids uninitialized warnings in callers */ \
59 : if (failureFatal && !native) \
60 : return xpc_qsThrow(cx, rv); \
61 : *ppThis = static_cast<_interface*>(static_cast<_base*>(native)); \
62 : return true; \
63 : } \
64 : \
65 : template <> \
66 : inline nsresult \
67 : xpc_qsUnwrapArg<_interface>(JSContext *cx, \
68 : jsval v, \
69 : _interface **ppArg, \
70 : nsISupports **ppArgRef, \
71 : jsval *vp) \
72 : { \
73 : nsresult rv; \
74 : nsISupports *native = castNativeArgFromWrapper(cx, v, _bit, ppArgRef, vp, \
75 : &rv); \
76 : if (NS_SUCCEEDED(rv)) \
77 : *ppArg = static_cast<_interface*>(static_cast<_base*>(native)); \
78 : return rv; \
79 : }
80 :
81 : #undef DOMCI_CASTABLE_INTERFACE
82 :
83 : #undef DOMCI_CASTABLE_INTERFACE
84 : #define DOMCI_CASTABLE_INTERFACE(_interface, _base, _bit, _extra) \
85 : DEFINE_UNWRAP_CAST(_interface, _base, _bit)
86 :
87 34690 : DOMCI_CASTABLE_INTERFACES(unused)
88 :
89 : #undef DOMCI_CASTABLE_INTERFACE
90 :
91 : // Ideally we'd just add nsGenericElement to the castable interfaces, but for
92 : // now nsDocumentFragment inherits from nsGenericElement (even though it's not
93 : // an Element) so we have to special-case nsGenericElement and use
94 : // nsIContent::IsElement().
95 : // FIXME: bug 563659.
96 : inline JSBool
97 10916 : castToElement(nsIContent *content, jsval val, nsGenericElement **ppInterface,
98 : jsval *pVal)
99 : {
100 10916 : if (!content->IsElement())
101 0 : return false;
102 10916 : *ppInterface = static_cast<nsGenericElement*>(content->AsElement());
103 10916 : *pVal = val;
104 10916 : return true;
105 : }
106 :
107 : template <>
108 : inline JSBool
109 10916 : xpc_qsUnwrapThis<nsGenericElement>(JSContext *cx,
110 : JSObject *obj,
111 : JSObject *callee,
112 : nsGenericElement **ppThis,
113 : nsISupports **pThisRef,
114 : jsval *pThisVal,
115 : XPCLazyCallContext *lccx,
116 : bool failureFatal)
117 : {
118 : nsIContent *content;
119 : jsval val;
120 : JSBool ok = xpc_qsUnwrapThis<nsIContent>(cx, obj, callee, &content,
121 : pThisRef, &val, lccx,
122 10916 : failureFatal);
123 10916 : if (ok) {
124 10916 : if (failureFatal || content)
125 10916 : ok = castToElement(content, val, ppThis, pThisVal);
126 10916 : if (failureFatal && !ok)
127 0 : xpc_qsThrow(cx, NS_ERROR_XPC_BAD_OP_ON_WN_PROTO);
128 : }
129 :
130 10916 : if (!failureFatal && (!ok || !content)) {
131 0 : ok = true;
132 0 : *ppThis = nsnull;
133 : }
134 :
135 10916 : return ok;
136 : }
137 :
138 : template <>
139 : inline nsresult
140 0 : xpc_qsUnwrapArg<nsGenericElement>(JSContext *cx,
141 : jsval v,
142 : nsGenericElement **ppArg,
143 : nsISupports **ppArgRef,
144 : jsval *vp)
145 : {
146 : nsIContent *content;
147 : jsval val;
148 0 : nsresult rv = xpc_qsUnwrapArg<nsIContent>(cx, v, &content, ppArgRef, &val);
149 0 : if (NS_SUCCEEDED(rv) && !castToElement(content, val, ppArg, vp))
150 0 : rv = NS_ERROR_XPC_BAD_CONVERT_JS;
151 0 : return rv;
152 : }
153 :
154 : inline nsISupports*
155 35 : ToSupports(nsContentList *p)
156 : {
157 35 : return static_cast<nsINodeList*>(p);
158 : }
159 :
160 : inline nsISupports*
161 16732 : ToCanonicalSupports(nsINode* p)
162 : {
163 16732 : return p;
164 : }
165 :
166 : inline nsISupports*
167 35 : ToCanonicalSupports(nsContentList *p)
168 : {
169 35 : return static_cast<nsINodeList*>(p);
170 : }
171 :
172 : #endif /* nsDOMQS_h__ */
|