1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/nsDOMClassInfo.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,505 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 sw=2 et tw=80: */ 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 nsDOMClassInfo_h___ 1.11 +#define nsDOMClassInfo_h___ 1.12 + 1.13 +#include "mozilla/Attributes.h" 1.14 +#include "nsIXPCScriptable.h" 1.15 +#include "nsIScriptGlobalObject.h" 1.16 +#include "nsIDOMScriptObjectFactory.h" 1.17 +#include "js/Id.h" 1.18 +#include "nsIXPConnect.h" 1.19 + 1.20 +#ifdef XP_WIN 1.21 +#undef GetClassName 1.22 +#endif 1.23 + 1.24 +class nsContentList; 1.25 +class nsDocument; 1.26 +struct nsGlobalNameStruct; 1.27 +class nsGlobalWindow; 1.28 +class nsIScriptSecurityManager; 1.29 + 1.30 +struct nsDOMClassInfoData; 1.31 + 1.32 +typedef nsIClassInfo* (*nsDOMClassInfoConstructorFnc) 1.33 + (nsDOMClassInfoData* aData); 1.34 + 1.35 +typedef nsresult (*nsDOMConstructorFunc)(nsISupports** aNewObject); 1.36 + 1.37 +struct nsDOMClassInfoData 1.38 +{ 1.39 + const char *mName; 1.40 + const char16_t *mNameUTF16; 1.41 + union { 1.42 + nsDOMClassInfoConstructorFnc mConstructorFptr; 1.43 + nsDOMClassInfoExternalConstructorFnc mExternalConstructorFptr; 1.44 + } u; 1.45 + 1.46 + nsIClassInfo *mCachedClassInfo; // low bit is set to 1 if external, 1.47 + // so be sure to mask if necessary! 1.48 + const nsIID *mProtoChainInterface; 1.49 + const nsIID **mInterfaces; 1.50 + uint32_t mScriptableFlags : 31; // flags must not use more than 31 bits! 1.51 + uint32_t mHasClassInterface : 1; 1.52 + uint32_t mInterfacesBitmap; 1.53 + bool mChromeOnly : 1; 1.54 + bool mAllowXBL : 1; 1.55 + bool mDisabled : 1; 1.56 +#ifdef DEBUG 1.57 + uint32_t mDebugID; 1.58 +#endif 1.59 +}; 1.60 + 1.61 +struct nsExternalDOMClassInfoData : public nsDOMClassInfoData 1.62 +{ 1.63 + const nsCID *mConstructorCID; 1.64 +}; 1.65 + 1.66 + 1.67 +// To be used with the nsDOMClassInfoData::mCachedClassInfo pointer. 1.68 +// The low bit is set when we created a generic helper for an external 1.69 +// (which holds on to the nsDOMClassInfoData). 1.70 +#define GET_CLEAN_CI_PTR(_ptr) (nsIClassInfo*)(uintptr_t(_ptr) & ~0x1) 1.71 +#define MARK_EXTERNAL(_ptr) (nsIClassInfo*)(uintptr_t(_ptr) | 0x1) 1.72 +#define IS_EXTERNAL(_ptr) (uintptr_t(_ptr) & 0x1) 1.73 + 1.74 + 1.75 +class nsDOMClassInfo : public nsXPCClassInfo 1.76 +{ 1.77 + friend class nsHTMLDocumentSH; 1.78 +public: 1.79 + nsDOMClassInfo(nsDOMClassInfoData* aData); 1.80 + virtual ~nsDOMClassInfo(); 1.81 + 1.82 + NS_DECL_NSIXPCSCRIPTABLE 1.83 + 1.84 + NS_DECL_ISUPPORTS 1.85 + 1.86 + NS_DECL_NSICLASSINFO 1.87 + 1.88 + // Helper method that returns a *non* refcounted pointer to a 1.89 + // helper. So please note, don't release this pointer, if you do, 1.90 + // you better make sure you've addreffed before release. 1.91 + // 1.92 + // Whaaaaa! I wanted to name this method GetClassInfo, but nooo, 1.93 + // some of Microsoft devstudio's headers #defines GetClassInfo to 1.94 + // GetClassInfoA so I can't, those $%#@^! bastards!!! What gives 1.95 + // them the right to do that? 1.96 + 1.97 + static nsIClassInfo* GetClassInfoInstance(nsDOMClassInfoData* aData); 1.98 + 1.99 + static void ShutDown(); 1.100 + 1.101 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.102 + { 1.103 + return new nsDOMClassInfo(aData); 1.104 + } 1.105 + 1.106 + /* 1.107 + * The following two functions exist because of the way that Xray wrappers 1.108 + * work. In order to allow scriptable helpers to define non-IDL defined but 1.109 + * still "safe" properties for Xray wrappers, we call into the scriptable 1.110 + * helper with |obj| being the wrapper. 1.111 + * 1.112 + * Ideally, that would be the end of the story, however due to complications 1.113 + * dealing with document.domain, it's possible to end up in a scriptable 1.114 + * helper with a wrapper, even though we should be treating the lookup as a 1.115 + * transparent one. 1.116 + * 1.117 + * Note: So ObjectIsNativeWrapper(cx, obj) check usually means "through xray 1.118 + * wrapper this part is not visible" while combined with 1.119 + * || xpc::WrapperFactory::XrayWrapperNotShadowing(obj) it means "through 1.120 + * xray wrapper it is visible only if it does not hide any native property." 1.121 + */ 1.122 + static bool ObjectIsNativeWrapper(JSContext* cx, JSObject* obj); 1.123 + 1.124 + static nsISupports *GetNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj); 1.125 + 1.126 + static nsIXPConnect *XPConnect() 1.127 + { 1.128 + return sXPConnect; 1.129 + } 1.130 + static nsIScriptSecurityManager *ScriptSecurityManager() 1.131 + { 1.132 + return sSecMan; 1.133 + } 1.134 + 1.135 +protected: 1.136 + friend nsIClassInfo* NS_GetDOMClassInfoInstance(nsDOMClassInfoID aID); 1.137 + 1.138 + const nsDOMClassInfoData* mData; 1.139 + 1.140 + virtual void PreserveWrapper(nsISupports *aNative) MOZ_OVERRIDE 1.141 + { 1.142 + } 1.143 + 1.144 + virtual uint32_t GetInterfacesBitmap() MOZ_OVERRIDE 1.145 + { 1.146 + return mData->mInterfacesBitmap; 1.147 + } 1.148 + 1.149 + static nsresult Init(); 1.150 + static nsresult RegisterClassProtos(int32_t aDOMClassInfoID); 1.151 + static nsresult RegisterExternalClasses(); 1.152 + nsresult ResolveConstructor(JSContext *cx, JSObject *obj, 1.153 + JSObject **objp); 1.154 + 1.155 + // Checks if id is a number and returns the number, if aIsNumber is 1.156 + // non-null it's set to true if the id is a number and false if it's 1.157 + // not a number. If id is not a number this method returns -1 1.158 + static int32_t GetArrayIndexFromId(JSContext *cx, JS::Handle<jsid> id, 1.159 + bool *aIsNumber = nullptr); 1.160 + 1.161 + static nsIXPConnect *sXPConnect; 1.162 + static nsIScriptSecurityManager *sSecMan; 1.163 + 1.164 + // nsIXPCScriptable code 1.165 + static nsresult DefineStaticJSVals(JSContext *cx); 1.166 + 1.167 + static bool sIsInitialized; 1.168 + 1.169 +public: 1.170 + static jsid sLocation_id; 1.171 + static jsid sConstructor_id; 1.172 + static jsid sLength_id; 1.173 + static jsid sItem_id; 1.174 + static jsid sNamedItem_id; 1.175 + static jsid sEnumerate_id; 1.176 + static jsid sTop_id; 1.177 + static jsid sDocument_id; 1.178 + static jsid sWrappedJSObject_id; 1.179 +}; 1.180 + 1.181 +// THIS ONE ISN'T SAFE!! It assumes that the private of the JSObject is 1.182 +// an nsISupports. 1.183 +inline 1.184 +const nsQueryInterface 1.185 +do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj) 1.186 +{ 1.187 + return nsQueryInterface(nsDOMClassInfo::GetNative(wrapper, obj)); 1.188 +} 1.189 + 1.190 +// THIS ONE ISN'T SAFE!! It assumes that the private of the JSObject is 1.191 +// an nsISupports. 1.192 +inline 1.193 +const nsQueryInterfaceWithError 1.194 +do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj, 1.195 + nsresult *aError) 1.196 + 1.197 +{ 1.198 + return nsQueryInterfaceWithError(nsDOMClassInfo::GetNative(wrapper, obj), 1.199 + aError); 1.200 +} 1.201 + 1.202 +inline 1.203 +nsQueryInterface 1.204 +do_QueryWrapper(JSContext *cx, JSObject *obj) 1.205 +{ 1.206 + nsISupports *native = 1.207 + nsDOMClassInfo::XPConnect()->GetNativeOfWrapper(cx, obj); 1.208 + return nsQueryInterface(native); 1.209 +} 1.210 + 1.211 +inline 1.212 +nsQueryInterfaceWithError 1.213 +do_QueryWrapper(JSContext *cx, JSObject *obj, nsresult* error) 1.214 +{ 1.215 + nsISupports *native = 1.216 + nsDOMClassInfo::XPConnect()->GetNativeOfWrapper(cx, obj); 1.217 + return nsQueryInterfaceWithError(native, error); 1.218 +} 1.219 + 1.220 + 1.221 +typedef nsDOMClassInfo nsDOMGenericSH; 1.222 + 1.223 +// Makes sure that the wrapper is preserved if new properties are added. 1.224 +class nsEventTargetSH : public nsDOMGenericSH 1.225 +{ 1.226 +protected: 1.227 + nsEventTargetSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData) 1.228 + { 1.229 + } 1.230 + 1.231 + virtual ~nsEventTargetSH() 1.232 + { 1.233 + } 1.234 +public: 1.235 + NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx, 1.236 + JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE; 1.237 + NS_IMETHOD AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.238 + JSObject *obj, jsid id, JS::Value *vp, bool *_retval) MOZ_OVERRIDE; 1.239 + 1.240 + virtual void PreserveWrapper(nsISupports *aNative) MOZ_OVERRIDE; 1.241 + 1.242 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.243 + { 1.244 + return new nsEventTargetSH(aData); 1.245 + } 1.246 +}; 1.247 + 1.248 +// Window scriptable helper 1.249 + 1.250 +class nsWindowSH : public nsDOMGenericSH 1.251 +{ 1.252 +protected: 1.253 + nsWindowSH(nsDOMClassInfoData *aData) : nsDOMGenericSH(aData) 1.254 + { 1.255 + } 1.256 + 1.257 + virtual ~nsWindowSH() 1.258 + { 1.259 + } 1.260 + 1.261 + static nsresult GlobalResolve(nsGlobalWindow *aWin, JSContext *cx, 1.262 + JS::Handle<JSObject*> obj, JS::Handle<jsid> id, 1.263 + JS::MutableHandle<JSPropertyDescriptor> desc); 1.264 + 1.265 + friend class nsGlobalWindow; 1.266 +public: 1.267 + NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx, 1.268 + JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE; 1.269 + NS_IMETHOD PostCreatePrototype(JSContext * cx, JSObject * proto) MOZ_OVERRIDE; 1.270 + NS_IMETHOD PostCreate(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.271 + JSObject *obj) MOZ_OVERRIDE; 1.272 + NS_IMETHOD Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.273 + JSObject *obj, bool *_retval) MOZ_OVERRIDE; 1.274 + NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.275 + JSObject *obj, jsid id, JSObject **objp, 1.276 + bool *_retval) MOZ_OVERRIDE; 1.277 + NS_IMETHOD OuterObject(nsIXPConnectWrappedNative *wrapper, JSContext * cx, 1.278 + JSObject * obj, JSObject * *_retval) MOZ_OVERRIDE; 1.279 + 1.280 + static bool NameStructEnabled(JSContext* aCx, nsGlobalWindow *aWin, 1.281 + const nsAString& aName, 1.282 + const nsGlobalNameStruct& aNameStruct); 1.283 + 1.284 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.285 + { 1.286 + return new nsWindowSH(aData); 1.287 + } 1.288 +}; 1.289 + 1.290 +// Location scriptable helper 1.291 + 1.292 +class nsLocationSH : public nsDOMGenericSH 1.293 +{ 1.294 +protected: 1.295 + nsLocationSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData) 1.296 + { 1.297 + } 1.298 + 1.299 + virtual ~nsLocationSH() 1.300 + { 1.301 + } 1.302 + 1.303 +public: 1.304 + NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx, 1.305 + JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE; 1.306 + NS_IMETHODIMP AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.307 + JSObject *obj, jsid id, JS::Value *vp, bool *_retval); 1.308 + 1.309 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.310 + { 1.311 + return new nsLocationSH(aData); 1.312 + } 1.313 +}; 1.314 + 1.315 + 1.316 +// Generic array scriptable helper 1.317 + 1.318 +class nsGenericArraySH : public nsDOMClassInfo 1.319 +{ 1.320 +protected: 1.321 + nsGenericArraySH(nsDOMClassInfoData* aData) : nsDOMClassInfo(aData) 1.322 + { 1.323 + } 1.324 + 1.325 + virtual ~nsGenericArraySH() 1.326 + { 1.327 + } 1.328 + 1.329 +public: 1.330 + NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.331 + JSObject *obj, jsid id, JSObject **objp, 1.332 + bool *_retval) MOZ_OVERRIDE; 1.333 + NS_IMETHOD Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.334 + JSObject *obj, bool *_retval) MOZ_OVERRIDE; 1.335 + 1.336 + virtual nsresult GetLength(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.337 + JS::Handle<JSObject*> obj, uint32_t *length); 1.338 + 1.339 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.340 + { 1.341 + return new nsGenericArraySH(aData); 1.342 + } 1.343 +}; 1.344 + 1.345 + 1.346 +// Array scriptable helper 1.347 + 1.348 +class nsArraySH : public nsGenericArraySH 1.349 +{ 1.350 +protected: 1.351 + nsArraySH(nsDOMClassInfoData* aData) : nsGenericArraySH(aData) 1.352 + { 1.353 + } 1.354 + 1.355 + virtual ~nsArraySH() 1.356 + { 1.357 + } 1.358 + 1.359 + // Subclasses need to override this, if the implementation can't fail it's 1.360 + // allowed to not set *aResult. 1.361 + virtual nsISupports* GetItemAt(nsISupports *aNative, uint32_t aIndex, 1.362 + nsWrapperCache **aCache, nsresult *aResult) = 0; 1.363 + 1.364 +public: 1.365 + NS_IMETHOD GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.366 + JSObject *obj, jsid id, JS::Value *vp, bool *_retval) MOZ_OVERRIDE; 1.367 + 1.368 +private: 1.369 + // Not implemented, nothing should create an instance of this class. 1.370 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData); 1.371 +}; 1.372 + 1.373 + 1.374 +// CSSRuleList helper 1.375 + 1.376 +class nsCSSRuleListSH : public nsArraySH 1.377 +{ 1.378 +protected: 1.379 + nsCSSRuleListSH(nsDOMClassInfoData* aData) : nsArraySH(aData) 1.380 + { 1.381 + } 1.382 + 1.383 + virtual ~nsCSSRuleListSH() 1.384 + { 1.385 + } 1.386 + 1.387 + virtual nsISupports* GetItemAt(nsISupports *aNative, uint32_t aIndex, 1.388 + nsWrapperCache **aCache, nsresult *aResult) MOZ_OVERRIDE; 1.389 + 1.390 +public: 1.391 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.392 + { 1.393 + return new nsCSSRuleListSH(aData); 1.394 + } 1.395 +}; 1.396 + 1.397 +// WebApps Storage helpers 1.398 + 1.399 +class nsStorage2SH : public nsDOMGenericSH 1.400 +{ 1.401 +protected: 1.402 + nsStorage2SH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData) 1.403 + { 1.404 + } 1.405 + 1.406 + virtual ~nsStorage2SH() 1.407 + { 1.408 + } 1.409 + 1.410 + NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.411 + JSObject *obj, jsid id, JSObject **objp, 1.412 + bool *_retval) MOZ_OVERRIDE; 1.413 + NS_IMETHOD SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.414 + JSObject *obj, jsid id, JS::Value *vp, bool *_retval) MOZ_OVERRIDE; 1.415 + NS_IMETHOD GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.416 + JSObject *obj, jsid id, JS::Value *vp, bool *_retval) MOZ_OVERRIDE; 1.417 + NS_IMETHOD DelProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.418 + JSObject *obj, jsid id, bool *_retval) MOZ_OVERRIDE; 1.419 + NS_IMETHOD NewEnumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.420 + JSObject *obj, uint32_t enum_op, JS::Value *statep, 1.421 + jsid *idp, bool *_retval) MOZ_OVERRIDE; 1.422 + 1.423 +public: 1.424 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.425 + { 1.426 + return new nsStorage2SH(aData); 1.427 + } 1.428 +}; 1.429 + 1.430 +// Event handler 'this' translator class, this is called by XPConnect 1.431 +// when a "function interface" (nsIDOMEventListener) is called, this 1.432 +// class extracts 'this' fomr the first argument to the called 1.433 +// function (nsIDOMEventListener::HandleEvent(in nsIDOMEvent)), this 1.434 +// class will pass back nsIDOMEvent::currentTarget to be used as 1.435 +// 'this'. 1.436 + 1.437 +class nsEventListenerThisTranslator : public nsIXPCFunctionThisTranslator 1.438 +{ 1.439 +public: 1.440 + nsEventListenerThisTranslator() 1.441 + { 1.442 + } 1.443 + 1.444 + virtual ~nsEventListenerThisTranslator() 1.445 + { 1.446 + } 1.447 + 1.448 + // nsISupports 1.449 + NS_DECL_ISUPPORTS 1.450 + 1.451 + // nsIXPCFunctionThisTranslator 1.452 + NS_DECL_NSIXPCFUNCTIONTHISTRANSLATOR 1.453 +}; 1.454 + 1.455 +class nsDOMConstructorSH : public nsDOMGenericSH 1.456 +{ 1.457 +protected: 1.458 + nsDOMConstructorSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData) 1.459 + { 1.460 + } 1.461 + 1.462 +public: 1.463 + NS_IMETHOD PreCreate(nsISupports *nativeObj, JSContext *cx, 1.464 + JSObject *globalObj, JSObject **parentObj) MOZ_OVERRIDE; 1.465 + NS_IMETHOD PostCreatePrototype(JSContext * cx, JSObject * proto) MOZ_OVERRIDE 1.466 + { 1.467 + return NS_OK; 1.468 + } 1.469 + NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.470 + JSObject *obj, jsid id, JSObject **objp, 1.471 + bool *_retval) MOZ_OVERRIDE; 1.472 + NS_IMETHOD Call(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.473 + JSObject *obj, const JS::CallArgs &args, bool *_retval) MOZ_OVERRIDE; 1.474 + 1.475 + NS_IMETHOD Construct(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.476 + JSObject *obj, const JS::CallArgs &args, bool *_retval) MOZ_OVERRIDE; 1.477 + 1.478 + NS_IMETHOD HasInstance(nsIXPConnectWrappedNative *wrapper, JSContext *cx, 1.479 + JSObject *obj, JS::Handle<JS::Value> val, bool *bp, 1.480 + bool *_retval); 1.481 + 1.482 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.483 + { 1.484 + return new nsDOMConstructorSH(aData); 1.485 + } 1.486 +}; 1.487 + 1.488 +class nsNonDOMObjectSH : public nsDOMGenericSH 1.489 +{ 1.490 +protected: 1.491 + nsNonDOMObjectSH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData) 1.492 + { 1.493 + } 1.494 + 1.495 + virtual ~nsNonDOMObjectSH() 1.496 + { 1.497 + } 1.498 + 1.499 +public: 1.500 + NS_IMETHOD GetFlags(uint32_t *aFlags) MOZ_OVERRIDE; 1.501 + 1.502 + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) 1.503 + { 1.504 + return new nsNonDOMObjectSH(aData); 1.505 + } 1.506 +}; 1.507 + 1.508 +#endif /* nsDOMClassInfo_h___ */