Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsDOMClassInfo.h
blob77e353f7317a654c0d7942defbaee5a1d7748c5e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
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/. */
7 #ifndef nsDOMClassInfo_h___
8 #define nsDOMClassInfo_h___
10 #include "mozilla/Attributes.h"
11 #include "nsIXPCScriptable.h"
12 #include "nsIScriptGlobalObject.h"
13 #include "nsIDOMScriptObjectFactory.h"
14 #include "js/Id.h"
15 #include "nsIXPConnect.h"
17 #ifdef XP_WIN
18 #undef GetClassName
19 #endif
21 class nsContentList;
22 class nsDocument;
23 struct nsGlobalNameStruct;
24 class nsGlobalWindow;
25 class nsIScriptSecurityManager;
27 struct nsDOMClassInfoData;
29 typedef nsIClassInfo* (*nsDOMClassInfoConstructorFnc)
30 (nsDOMClassInfoData* aData);
32 typedef nsresult (*nsDOMConstructorFunc)(nsISupports** aNewObject);
34 struct nsDOMClassInfoData
36 const char *mName;
37 const char16_t *mNameUTF16;
38 union {
39 nsDOMClassInfoConstructorFnc mConstructorFptr;
40 nsDOMClassInfoExternalConstructorFnc mExternalConstructorFptr;
41 } u;
43 nsIClassInfo *mCachedClassInfo; // low bit is set to 1 if external,
44 // so be sure to mask if necessary!
45 const nsIID *mProtoChainInterface;
46 const nsIID **mInterfaces;
47 uint32_t mScriptableFlags : 31; // flags must not use more than 31 bits!
48 uint32_t mHasClassInterface : 1;
49 uint32_t mInterfacesBitmap;
50 bool mChromeOnly : 1;
51 bool mAllowXBL : 1;
52 bool mDisabled : 1;
53 #ifdef DEBUG
54 uint32_t mDebugID;
55 #endif
58 struct nsExternalDOMClassInfoData : public nsDOMClassInfoData
60 const nsCID *mConstructorCID;
64 // To be used with the nsDOMClassInfoData::mCachedClassInfo pointer.
65 // The low bit is set when we created a generic helper for an external
66 // (which holds on to the nsDOMClassInfoData).
67 #define GET_CLEAN_CI_PTR(_ptr) (nsIClassInfo*)(uintptr_t(_ptr) & ~0x1)
68 #define MARK_EXTERNAL(_ptr) (nsIClassInfo*)(uintptr_t(_ptr) | 0x1)
69 #define IS_EXTERNAL(_ptr) (uintptr_t(_ptr) & 0x1)
72 class nsDOMClassInfo : public nsXPCClassInfo
74 friend class nsHTMLDocumentSH;
76 protected:
77 virtual ~nsDOMClassInfo();
79 public:
80 explicit nsDOMClassInfo(nsDOMClassInfoData* aData);
82 NS_DECL_NSIXPCSCRIPTABLE
84 NS_DECL_ISUPPORTS
86 NS_DECL_NSICLASSINFO
88 // Helper method that returns a *non* refcounted pointer to a
89 // helper. So please note, don't release this pointer, if you do,
90 // you better make sure you've addreffed before release.
92 // Whaaaaa! I wanted to name this method GetClassInfo, but nooo,
93 // some of Microsoft devstudio's headers #defines GetClassInfo to
94 // GetClassInfoA so I can't, those $%#@^! bastards!!! What gives
95 // them the right to do that?
97 static nsIClassInfo* GetClassInfoInstance(nsDOMClassInfoData* aData);
99 static nsresult Init();
100 static void ShutDown();
102 static nsIClassInfo *doCreate(nsDOMClassInfoData* aData)
104 return new nsDOMClassInfo(aData);
108 * The following two functions exist because of the way that Xray wrappers
109 * work. In order to allow scriptable helpers to define non-IDL defined but
110 * still "safe" properties for Xray wrappers, we call into the scriptable
111 * helper with |obj| being the wrapper.
113 * Ideally, that would be the end of the story, however due to complications
114 * dealing with document.domain, it's possible to end up in a scriptable
115 * helper with a wrapper, even though we should be treating the lookup as a
116 * transparent one.
118 * Note: So ObjectIsNativeWrapper(cx, obj) check usually means "through xray
119 * wrapper this part is not visible" while combined with
120 * || xpc::WrapperFactory::XrayWrapperNotShadowing(obj) it means "through
121 * xray wrapper it is visible only if it does not hide any native property."
123 static bool ObjectIsNativeWrapper(JSContext* cx, JSObject* obj);
125 static nsISupports *GetNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj);
127 static nsIXPConnect *XPConnect()
129 return sXPConnect;
132 protected:
133 friend nsIClassInfo* NS_GetDOMClassInfoInstance(nsDOMClassInfoID aID);
135 const nsDOMClassInfoData* mData;
137 virtual void PreserveWrapper(nsISupports *aNative) MOZ_OVERRIDE
141 virtual uint32_t GetInterfacesBitmap() MOZ_OVERRIDE
143 return mData->mInterfacesBitmap;
146 static nsresult RegisterClassProtos(int32_t aDOMClassInfoID);
147 static nsresult RegisterExternalClasses();
148 nsresult ResolveConstructor(JSContext *cx, JSObject *obj,
149 JSObject **objp);
151 static nsIXPConnect *sXPConnect;
153 // nsIXPCScriptable code
154 static nsresult DefineStaticJSVals(JSContext *cx);
156 static bool sIsInitialized;
158 public:
159 static jsid sLocation_id;
160 static jsid sConstructor_id;
161 static jsid sTop_id;
162 static jsid sDocument_id;
163 static jsid sWrappedJSObject_id;
166 // THIS ONE ISN'T SAFE!! It assumes that the private of the JSObject is
167 // an nsISupports.
168 inline
169 const nsQueryInterface
170 do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj)
172 return nsQueryInterface(nsDOMClassInfo::GetNative(wrapper, obj));
175 // THIS ONE ISN'T SAFE!! It assumes that the private of the JSObject is
176 // an nsISupports.
177 inline
178 const nsQueryInterfaceWithError
179 do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj,
180 nsresult *aError)
183 return nsQueryInterfaceWithError(nsDOMClassInfo::GetNative(wrapper, obj),
184 aError);
187 inline
188 nsQueryInterface
189 do_QueryWrapper(JSContext *cx, JSObject *obj)
191 nsISupports *native =
192 nsDOMClassInfo::XPConnect()->GetNativeOfWrapper(cx, obj);
193 return nsQueryInterface(native);
196 inline
197 nsQueryInterfaceWithError
198 do_QueryWrapper(JSContext *cx, JSObject *obj, nsresult* error)
200 nsISupports *native =
201 nsDOMClassInfo::XPConnect()->GetNativeOfWrapper(cx, obj);
202 return nsQueryInterfaceWithError(native, error);
206 typedef nsDOMClassInfo nsDOMGenericSH;
208 // Makes sure that the wrapper is preserved if new properties are added.
209 class nsEventTargetSH : public nsDOMGenericSH
211 protected:
212 explicit nsEventTargetSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData)
216 virtual ~nsEventTargetSH()
219 public:
220 NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx,
221 JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE;
222 NS_IMETHOD AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
223 JSObject *obj, jsid id, JS::Value *vp, bool *_retval) MOZ_OVERRIDE;
225 virtual void PreserveWrapper(nsISupports *aNative) MOZ_OVERRIDE;
227 static nsIClassInfo *doCreate(nsDOMClassInfoData* aData)
229 return new nsEventTargetSH(aData);
233 // Window scriptable helper
235 class nsWindowSH : public nsDOMGenericSH
237 protected:
238 explicit nsWindowSH(nsDOMClassInfoData *aData) : nsDOMGenericSH(aData)
242 virtual ~nsWindowSH()
246 static nsresult GlobalResolve(nsGlobalWindow *aWin, JSContext *cx,
247 JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
248 JS::MutableHandle<JSPropertyDescriptor> desc);
250 friend class nsGlobalWindow;
251 public:
252 NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx,
253 JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE;
254 NS_IMETHOD PostCreatePrototype(JSContext * cx, JSObject * proto) MOZ_OVERRIDE;
255 NS_IMETHOD PostCreate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
256 JSObject *obj) MOZ_OVERRIDE;
257 NS_IMETHOD Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
258 JSObject *obj, bool *_retval) MOZ_OVERRIDE;
259 NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
260 JSObject *obj, jsid id, JSObject **objp,
261 bool *_retval) MOZ_OVERRIDE;
262 NS_IMETHOD OuterObject(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
263 JSObject * obj, JSObject * *_retval) MOZ_OVERRIDE;
265 static bool NameStructEnabled(JSContext* aCx, nsGlobalWindow *aWin,
266 const nsAString& aName,
267 const nsGlobalNameStruct& aNameStruct);
269 static nsIClassInfo *doCreate(nsDOMClassInfoData* aData)
271 return new nsWindowSH(aData);
276 // Event handler 'this' translator class, this is called by XPConnect
277 // when a "function interface" (nsIDOMEventListener) is called, this
278 // class extracts 'this' fomr the first argument to the called
279 // function (nsIDOMEventListener::HandleEvent(in nsIDOMEvent)), this
280 // class will pass back nsIDOMEvent::currentTarget to be used as
281 // 'this'.
283 class nsEventListenerThisTranslator : public nsIXPCFunctionThisTranslator
285 virtual ~nsEventListenerThisTranslator()
289 public:
290 nsEventListenerThisTranslator()
294 // nsISupports
295 NS_DECL_ISUPPORTS
297 // nsIXPCFunctionThisTranslator
298 NS_DECL_NSIXPCFUNCTIONTHISTRANSLATOR
301 class nsDOMConstructorSH : public nsDOMGenericSH
303 protected:
304 explicit nsDOMConstructorSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData)
308 public:
309 NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx,
310 JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE;
311 NS_IMETHOD PostCreatePrototype(JSContext * cx, JSObject * proto) MOZ_OVERRIDE
313 return NS_OK;
315 NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
316 JSObject *obj, jsid id, JSObject **objp,
317 bool *_retval) MOZ_OVERRIDE;
318 NS_IMETHOD Call(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
319 JSObject *obj, const JS::CallArgs &args, bool *_retval) MOZ_OVERRIDE;
321 NS_IMETHOD Construct(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
322 JSObject *obj, const JS::CallArgs &args, bool *_retval) MOZ_OVERRIDE;
324 NS_IMETHOD HasInstance(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
325 JSObject *obj, JS::Handle<JS::Value> val, bool *bp,
326 bool *_retval);
328 static nsIClassInfo *doCreate(nsDOMClassInfoData* aData)
330 return new nsDOMConstructorSH(aData);
334 class nsNonDOMObjectSH : public nsDOMGenericSH
336 protected:
337 explicit nsNonDOMObjectSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData)
341 virtual ~nsNonDOMObjectSH()
345 public:
346 NS_IMETHOD GetFlags(uint32_t *aFlags) MOZ_OVERRIDE;
348 static nsIClassInfo *doCreate(nsDOMClassInfoData* aData)
350 return new nsNonDOMObjectSH(aData);
354 #endif /* nsDOMClassInfo_h___ */