dom/base/nsDOMClassInfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/nsDOMClassInfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,3956 @@
     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=78: */
     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 +#include "mozilla/ArrayUtils.h"
    1.11 +// On top because they include basictypes.h:
    1.12 +#include "mozilla/dom/SmsFilter.h"
    1.13 +
    1.14 +#ifdef XP_WIN
    1.15 +#undef GetClassName
    1.16 +#endif
    1.17 +
    1.18 +// JavaScript includes
    1.19 +#include "jsapi.h"
    1.20 +#include "jsfriendapi.h"
    1.21 +#include "WrapperFactory.h"
    1.22 +#include "AccessCheck.h"
    1.23 +#include "XrayWrapper.h"
    1.24 +
    1.25 +#include "xpcpublic.h"
    1.26 +#include "xpcprivate.h"
    1.27 +#include "XPCWrapper.h"
    1.28 +
    1.29 +#include "mozilla/DOMEventTargetHelper.h"
    1.30 +#include "mozilla/dom/RegisterBindings.h"
    1.31 +
    1.32 +#include "nscore.h"
    1.33 +#include "nsDOMClassInfo.h"
    1.34 +#include "nsCRT.h"
    1.35 +#include "nsCRTGlue.h"
    1.36 +#include "nsICategoryManager.h"
    1.37 +#include "nsIComponentRegistrar.h"
    1.38 +#include "nsXPCOM.h"
    1.39 +#include "nsISupportsPrimitives.h"
    1.40 +#include "nsIXPConnect.h"
    1.41 +#include "nsIXPCSecurityManager.h"
    1.42 +#include "xptcall.h"
    1.43 +#include "nsTArray.h"
    1.44 +#include "nsDocument.h" // nsDOMStyleSheetList
    1.45 +#include "nsDOMBlobBuilder.h"
    1.46 +
    1.47 +// General helper includes
    1.48 +#include "nsGlobalWindow.h"
    1.49 +#include "nsIContent.h"
    1.50 +#include "nsIDocument.h"
    1.51 +#include "nsIDOMDocument.h"
    1.52 +#include "nsIDOMEvent.h"
    1.53 +#include "nsIDOMEventListener.h"
    1.54 +#include "nsContentUtils.h"
    1.55 +#include "nsCxPusher.h"
    1.56 +#include "nsIDOMWindowUtils.h"
    1.57 +#include "nsIDOMGlobalPropertyInitializer.h"
    1.58 +#include "nsLocation.h"
    1.59 +#include "mozilla/Attributes.h"
    1.60 +#include "mozilla/Telemetry.h"
    1.61 +
    1.62 +// Window scriptable helper includes
    1.63 +#include "nsIDocShell.h"
    1.64 +#include "nsIScriptExternalNameSet.h"
    1.65 +#include "nsJSUtils.h"
    1.66 +#include "nsScriptNameSpaceManager.h"
    1.67 +#include "nsIJSNativeInitializer.h"
    1.68 +#include "nsJSEnvironment.h"
    1.69 +
    1.70 +// DOM base includes
    1.71 +#include "nsIDOMLocation.h"
    1.72 +#include "nsIDOMWindow.h"
    1.73 +#include "nsPIDOMWindow.h"
    1.74 +#include "nsIDOMJSWindow.h"
    1.75 +#include "nsIDOMChromeWindow.h"
    1.76 +#include "nsIDOMConstructor.h"
    1.77 +
    1.78 +// DOM core includes
    1.79 +#include "nsError.h"
    1.80 +#include "nsIDOMUserDataHandler.h"
    1.81 +#include "nsIDOMXULButtonElement.h"
    1.82 +#include "nsIDOMXULCheckboxElement.h"
    1.83 +#include "nsIDOMXULPopupElement.h"
    1.84 +
    1.85 +// Event related includes
    1.86 +#include "nsIDOMEventTarget.h"
    1.87 +
    1.88 +// CSS related includes
    1.89 +#include "nsCSSRules.h"
    1.90 +#include "nsIDOMCSSRule.h"
    1.91 +#include "nsICSSRuleList.h"
    1.92 +#include "nsAutoPtr.h"
    1.93 +#include "nsMemory.h"
    1.94 +
    1.95 +// Tranformiix
    1.96 +#include "nsIXSLTProcessor.h"
    1.97 +#include "nsIXSLTProcessorPrivate.h"
    1.98 +
    1.99 +// includes needed for the prototype chain interfaces
   1.100 +#include "nsIDOMCSSCharsetRule.h"
   1.101 +#include "nsIDOMCSSImportRule.h"
   1.102 +#include "nsIDOMCSSMediaRule.h"
   1.103 +#include "nsIDOMCSSFontFaceRule.h"
   1.104 +#include "nsIDOMCSSMozDocumentRule.h"
   1.105 +#include "nsIDOMCSSSupportsRule.h"
   1.106 +#include "nsIDOMMozCSSKeyframeRule.h"
   1.107 +#include "nsIDOMMozCSSKeyframesRule.h"
   1.108 +#include "nsIDOMCSSPageRule.h"
   1.109 +#include "nsIDOMCSSStyleRule.h"
   1.110 +#include "nsIDOMCSSStyleSheet.h"
   1.111 +#include "nsIDOMXULCommandDispatcher.h"
   1.112 +#include "nsIControllers.h"
   1.113 +#include "nsIBoxObject.h"
   1.114 +#ifdef MOZ_XUL
   1.115 +#include "nsITreeSelection.h"
   1.116 +#include "nsITreeContentView.h"
   1.117 +#include "nsITreeView.h"
   1.118 +#include "nsIXULTemplateBuilder.h"
   1.119 +#include "nsITreeColumns.h"
   1.120 +#endif
   1.121 +#include "nsIDOMXPathExpression.h"
   1.122 +#include "nsIDOMNSXPathExpression.h"
   1.123 +#include "nsIDOMXPathNSResolver.h"
   1.124 +#include "nsIDOMXPathResult.h"
   1.125 +
   1.126 +#include "nsIDOMSVGNumber.h"
   1.127 +
   1.128 +// Storage includes
   1.129 +#include "nsIDOMStorage.h"
   1.130 +#include "nsPIDOMStorage.h"
   1.131 +
   1.132 +// Drag and drop
   1.133 +#include "nsIDOMFile.h"
   1.134 +#include "nsDOMBlobBuilder.h" // nsDOMMultipartFile
   1.135 +
   1.136 +#include "nsIEventListenerService.h"
   1.137 +#include "nsIMessageManager.h"
   1.138 +
   1.139 +#include "mozilla/dom/TouchEvent.h"
   1.140 +
   1.141 +#include "nsWrapperCacheInlines.h"
   1.142 +#include "mozilla/dom/HTMLCollectionBinding.h"
   1.143 +
   1.144 +#include "nsIDOMMobileMessageManager.h"
   1.145 +#include "nsIDOMMozSmsMessage.h"
   1.146 +#include "nsIDOMMozMmsMessage.h"
   1.147 +#include "nsIDOMSmsFilter.h"
   1.148 +#include "nsIDOMSmsSegmentInfo.h"
   1.149 +#include "nsIDOMMozMobileMessageThread.h"
   1.150 +
   1.151 +#ifdef MOZ_B2G_RIL
   1.152 +#include "nsIDOMMobileConnection.h"
   1.153 +#endif // MOZ_B2G_RIL
   1.154 +
   1.155 +#ifdef MOZ_B2G_FM
   1.156 +#include "FMRadio.h"
   1.157 +#endif
   1.158 +
   1.159 +#include "nsIDOMGlobalObjectConstructor.h"
   1.160 +#include "nsDebug.h"
   1.161 +
   1.162 +#include "mozilla/dom/BindingUtils.h"
   1.163 +#include "mozilla/Likely.h"
   1.164 +#include "WindowNamedPropertiesHandler.h"
   1.165 +#include "nsIInterfaceInfoManager.h"
   1.166 +#include "mozilla/dom/EventTargetBinding.h"
   1.167 +#include "mozilla/dom/WindowBinding.h"
   1.168 +
   1.169 +#ifdef MOZ_TIME_MANAGER
   1.170 +#include "TimeManager.h"
   1.171 +#endif
   1.172 +
   1.173 +using namespace mozilla;
   1.174 +using namespace mozilla::dom;
   1.175 +
   1.176 +static NS_DEFINE_CID(kDOMSOF_CID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
   1.177 +
   1.178 +// NOTE: DEFAULT_SCRIPTABLE_FLAGS and DOM_DEFAULT_SCRIPTABLE_FLAGS
   1.179 +//       are defined in nsIDOMClassInfo.h.
   1.180 +
   1.181 +#define WINDOW_SCRIPTABLE_FLAGS                                               \
   1.182 + (nsIXPCScriptable::WANT_PRECREATE |                                          \
   1.183 +  nsIXPCScriptable::WANT_POSTCREATE |                                         \
   1.184 +  nsIXPCScriptable::WANT_ENUMERATE |                                          \
   1.185 +  nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE |                               \
   1.186 +  nsIXPCScriptable::IS_GLOBAL_OBJECT |                                        \
   1.187 +  nsIXPCScriptable::WANT_OUTER_OBJECT)
   1.188 +
   1.189 +#define ARRAY_SCRIPTABLE_FLAGS                                                \
   1.190 +  (DOM_DEFAULT_SCRIPTABLE_FLAGS       |                                       \
   1.191 +   nsIXPCScriptable::WANT_GETPROPERTY |                                       \
   1.192 +   nsIXPCScriptable::WANT_ENUMERATE)
   1.193 +
   1.194 +#define EVENTTARGET_SCRIPTABLE_FLAGS                                          \
   1.195 +  (DOM_DEFAULT_SCRIPTABLE_FLAGS       |                                       \
   1.196 +   nsIXPCScriptable::WANT_ADDPROPERTY)
   1.197 +
   1.198 +#define DOMCLASSINFO_STANDARD_FLAGS                                           \
   1.199 +  (nsIClassInfo::MAIN_THREAD_ONLY |                                           \
   1.200 +   nsIClassInfo::DOM_OBJECT       |                                           \
   1.201 +   nsIClassInfo::SINGLETON_CLASSINFO)
   1.202 +
   1.203 +
   1.204 +#ifdef DEBUG
   1.205 +#define NS_DEFINE_CLASSINFO_DATA_DEBUG(_class)                                \
   1.206 +    eDOMClassInfo_##_class##_id,
   1.207 +#else
   1.208 +#define NS_DEFINE_CLASSINFO_DATA_DEBUG(_class)                                \
   1.209 +  // nothing
   1.210 +#endif
   1.211 +
   1.212 +/**
   1.213 + * To generate the bitmap for a class that we're sure doesn't implement any of
   1.214 + * the interfaces in DOMCI_CASTABLE_INTERFACES.
   1.215 + */
   1.216 +#define DOMCI_DATA_NO_CLASS(_dom_class)                                       \
   1.217 +const uint32_t kDOMClassInfo_##_dom_class##_interfaces =                      \
   1.218 +  0;
   1.219 +
   1.220 +DOMCI_DATA_NO_CLASS(ContentFrameMessageManager)
   1.221 +DOMCI_DATA_NO_CLASS(ChromeMessageBroadcaster)
   1.222 +DOMCI_DATA_NO_CLASS(ChromeMessageSender)
   1.223 +
   1.224 +DOMCI_DATA_NO_CLASS(DOMPrototype)
   1.225 +DOMCI_DATA_NO_CLASS(DOMConstructor)
   1.226 +
   1.227 +DOMCI_DATA_NO_CLASS(UserDataHandler)
   1.228 +DOMCI_DATA_NO_CLASS(XULControlElement)
   1.229 +DOMCI_DATA_NO_CLASS(XULLabeledControlElement)
   1.230 +DOMCI_DATA_NO_CLASS(XULButtonElement)
   1.231 +DOMCI_DATA_NO_CLASS(XULCheckboxElement)
   1.232 +DOMCI_DATA_NO_CLASS(XULPopupElement)
   1.233 +
   1.234 +#define NS_DEFINE_CLASSINFO_DATA_HELPER(_class, _helper, _flags,              \
   1.235 +                                        _chromeOnly, _allowXBL)               \
   1.236 +  { #_class,                                                                  \
   1.237 +    nullptr,                                                                  \
   1.238 +    { _helper::doCreate },                                                    \
   1.239 +    nullptr,                                                                  \
   1.240 +    nullptr,                                                                  \
   1.241 +    nullptr,                                                                  \
   1.242 +    _flags,                                                                   \
   1.243 +    true,                                                                     \
   1.244 +    0,                                                                        \
   1.245 +    _chromeOnly,                                                              \
   1.246 +    _allowXBL,                                                                \
   1.247 +    false,                                                                    \
   1.248 +    NS_DEFINE_CLASSINFO_DATA_DEBUG(_class)                                    \
   1.249 +  },
   1.250 +
   1.251 +#define NS_DEFINE_CLASSINFO_DATA(_class, _helper, _flags)                     \
   1.252 +  NS_DEFINE_CLASSINFO_DATA_HELPER(_class, _helper, _flags, false, false)
   1.253 +
   1.254 +#define NS_DEFINE_CHROME_ONLY_CLASSINFO_DATA(_class, _helper, _flags)         \
   1.255 +  NS_DEFINE_CLASSINFO_DATA_HELPER(_class, _helper, _flags, true, false)
   1.256 +
   1.257 +#define NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(_class, _helper, _flags)          \
   1.258 +  NS_DEFINE_CLASSINFO_DATA_HELPER(_class, _helper, _flags, true, true)
   1.259 +
   1.260 +
   1.261 +// This list of NS_DEFINE_CLASSINFO_DATA macros is what gives the DOM
   1.262 +// classes their correct behavior when used through XPConnect. The
   1.263 +// arguments that are passed to NS_DEFINE_CLASSINFO_DATA are
   1.264 +//
   1.265 +// 1. Class name as it should appear in JavaScript, this name is also
   1.266 +//    used to find the id of the class in nsDOMClassInfo
   1.267 +//    (i.e. e<classname>_id)
   1.268 +// 2. Scriptable helper class
   1.269 +// 3. nsIClassInfo/nsIXPCScriptable flags (i.e. for GetScriptableFlags)
   1.270 +
   1.271 +static nsDOMClassInfoData sClassInfoData[] = {
   1.272 +  // Base classes
   1.273 +
   1.274 +  // The Window class lets you QI into interfaces that are not in the
   1.275 +  // flattened set (i.e. nsIXPCScriptable::CLASSINFO_INTERFACES_ONLY
   1.276 +  // is not set), because of this make sure all scriptable interfaces
   1.277 +  // that are implemented by nsGlobalWindow can securely be exposed
   1.278 +  // to JS.
   1.279 +
   1.280 +
   1.281 +  NS_DEFINE_CLASSINFO_DATA(Window, nsWindowSH,
   1.282 +                           DEFAULT_SCRIPTABLE_FLAGS |
   1.283 +                           WINDOW_SCRIPTABLE_FLAGS)
   1.284 +
   1.285 +  NS_DEFINE_CLASSINFO_DATA(Location, nsLocationSH,
   1.286 +                           ((DOM_DEFAULT_SCRIPTABLE_FLAGS |
   1.287 +                             nsIXPCScriptable::WANT_ADDPROPERTY) &
   1.288 +                            ~nsIXPCScriptable::ALLOW_PROP_MODS_TO_PROTOTYPE))
   1.289 +
   1.290 +  NS_DEFINE_CLASSINFO_DATA(DOMPrototype, nsDOMConstructorSH,
   1.291 +                           DOM_BASE_SCRIPTABLE_FLAGS |
   1.292 +                           nsIXPCScriptable::WANT_PRECREATE |
   1.293 +                           nsIXPCScriptable::WANT_NEWRESOLVE |
   1.294 +                           nsIXPCScriptable::WANT_HASINSTANCE |
   1.295 +                           nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE)
   1.296 +  NS_DEFINE_CLASSINFO_DATA(DOMConstructor, nsDOMConstructorSH,
   1.297 +                           DOM_BASE_SCRIPTABLE_FLAGS |
   1.298 +                           nsIXPCScriptable::WANT_PRECREATE |
   1.299 +                           nsIXPCScriptable::WANT_NEWRESOLVE |
   1.300 +                           nsIXPCScriptable::WANT_HASINSTANCE |
   1.301 +                           nsIXPCScriptable::WANT_CALL |
   1.302 +                           nsIXPCScriptable::WANT_CONSTRUCT |
   1.303 +                           nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE)
   1.304 +
   1.305 +  // Misc Core related classes
   1.306 +
   1.307 +  // CSS classes
   1.308 +  NS_DEFINE_CLASSINFO_DATA(CSSStyleRule, nsDOMGenericSH,
   1.309 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.310 +  NS_DEFINE_CLASSINFO_DATA(CSSCharsetRule, nsDOMGenericSH,
   1.311 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.312 +  NS_DEFINE_CLASSINFO_DATA(CSSImportRule, nsDOMGenericSH,
   1.313 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.314 +  NS_DEFINE_CLASSINFO_DATA(CSSMediaRule, nsDOMGenericSH,
   1.315 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.316 +  NS_DEFINE_CLASSINFO_DATA(CSSNameSpaceRule, nsDOMGenericSH,
   1.317 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.318 +  NS_DEFINE_CLASSINFO_DATA(CSSRuleList, nsCSSRuleListSH,
   1.319 +                           ARRAY_SCRIPTABLE_FLAGS)
   1.320 +  NS_DEFINE_CLASSINFO_DATA(CSSStyleSheet, nsDOMGenericSH,
   1.321 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.322 +
   1.323 +  // XUL classes
   1.324 +#ifdef MOZ_XUL
   1.325 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULCommandDispatcher, nsDOMGenericSH,
   1.326 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.327 +#endif
   1.328 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULControllers, nsNonDOMObjectSH,
   1.329 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.330 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(BoxObject, nsDOMGenericSH,
   1.331 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.332 +#ifdef MOZ_XUL
   1.333 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(TreeSelection, nsDOMGenericSH,
   1.334 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.335 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(TreeContentView, nsDOMGenericSH,
   1.336 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.337 +#endif
   1.338 +
   1.339 +  // DOM Chrome Window class.
   1.340 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(ChromeWindow, nsWindowSH,
   1.341 +                                      DEFAULT_SCRIPTABLE_FLAGS |
   1.342 +                                      WINDOW_SCRIPTABLE_FLAGS)
   1.343 +
   1.344 +#ifdef MOZ_XUL
   1.345 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULTemplateBuilder, nsDOMGenericSH,
   1.346 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.347 +
   1.348 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULTreeBuilder, nsDOMGenericSH,
   1.349 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.350 +#endif
   1.351 +
   1.352 +#ifdef MOZ_XUL
   1.353 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(TreeColumn, nsDOMGenericSH,
   1.354 +                                      DEFAULT_SCRIPTABLE_FLAGS)
   1.355 +#endif
   1.356 +
   1.357 +  NS_DEFINE_CLASSINFO_DATA(CSSMozDocumentRule, nsDOMGenericSH,
   1.358 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.359 +
   1.360 +  NS_DEFINE_CLASSINFO_DATA(CSSSupportsRule, nsDOMGenericSH,
   1.361 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.362 +
   1.363 +  // other SVG classes
   1.364 +  NS_DEFINE_CLASSINFO_DATA(SVGNumber, nsDOMGenericSH,
   1.365 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.366 +
   1.367 +  NS_DEFINE_CLASSINFO_DATA(WindowUtils, nsDOMGenericSH,
   1.368 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.369 +
   1.370 +  NS_DEFINE_CLASSINFO_DATA(XSLTProcessor, nsDOMGenericSH,
   1.371 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.372 +
   1.373 +  NS_DEFINE_CLASSINFO_DATA(XPathExpression, nsDOMGenericSH,
   1.374 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.375 +  NS_DEFINE_CLASSINFO_DATA(XPathNSResolver, nsDOMGenericSH,
   1.376 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.377 +  NS_DEFINE_CLASSINFO_DATA(XPathResult, nsDOMGenericSH,
   1.378 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.379 +
   1.380 +  // WhatWG Storage
   1.381 +
   1.382 +  // mrbkap says we don't need WANT_ADDPROPERTY on Storage objects
   1.383 +  // since a call to addProperty() is always followed by a call to
   1.384 +  // setProperty(), except in the case when a getter or setter is set
   1.385 +  // for a property. But we don't care about getters or setters here.
   1.386 +  NS_DEFINE_CLASSINFO_DATA(Storage, nsStorage2SH,
   1.387 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS |
   1.388 +                           nsIXPCScriptable::WANT_NEWRESOLVE |
   1.389 +                           nsIXPCScriptable::WANT_GETPROPERTY |
   1.390 +                           nsIXPCScriptable::WANT_SETPROPERTY |
   1.391 +                           nsIXPCScriptable::WANT_DELPROPERTY |
   1.392 +                           nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |
   1.393 +                           nsIXPCScriptable::WANT_NEWENUMERATE)
   1.394 +
   1.395 +  NS_DEFINE_CLASSINFO_DATA(Blob, nsDOMGenericSH,
   1.396 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.397 +  NS_DEFINE_CLASSINFO_DATA(File, nsDOMGenericSH,
   1.398 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.399 +
   1.400 +  NS_DEFINE_CLASSINFO_DATA(ModalContentWindow, nsWindowSH,
   1.401 +                           DEFAULT_SCRIPTABLE_FLAGS |
   1.402 +                           WINDOW_SCRIPTABLE_FLAGS)
   1.403 +
   1.404 +  NS_DEFINE_CLASSINFO_DATA(MozMobileMessageManager, nsDOMGenericSH,
   1.405 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.406 +
   1.407 +  NS_DEFINE_CLASSINFO_DATA(MozSmsMessage, nsDOMGenericSH,
   1.408 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.409 +
   1.410 +  NS_DEFINE_CLASSINFO_DATA(MozMmsMessage, nsDOMGenericSH,
   1.411 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.412 +
   1.413 +  NS_DEFINE_CLASSINFO_DATA(MozSmsFilter, nsDOMGenericSH,
   1.414 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.415 +
   1.416 +  NS_DEFINE_CLASSINFO_DATA(MozSmsSegmentInfo, nsDOMGenericSH,
   1.417 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.418 +
   1.419 +  NS_DEFINE_CLASSINFO_DATA(MozMobileMessageThread, nsDOMGenericSH,
   1.420 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.421 +
   1.422 +#ifdef MOZ_B2G_RIL
   1.423 +  NS_DEFINE_CLASSINFO_DATA(MozMobileConnection, nsDOMGenericSH,
   1.424 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.425 +#endif
   1.426 +
   1.427 +  NS_DEFINE_CLASSINFO_DATA(CSSFontFaceRule, nsDOMGenericSH,
   1.428 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.429 +
   1.430 +  NS_DEFINE_CHROME_ONLY_CLASSINFO_DATA(ContentFrameMessageManager, nsEventTargetSH,
   1.431 +                                       DOM_DEFAULT_SCRIPTABLE_FLAGS |
   1.432 +                                       nsIXPCScriptable::IS_GLOBAL_OBJECT)
   1.433 +  NS_DEFINE_CHROME_ONLY_CLASSINFO_DATA(ChromeMessageBroadcaster, nsDOMGenericSH,
   1.434 +                                       DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.435 +  NS_DEFINE_CHROME_ONLY_CLASSINFO_DATA(ChromeMessageSender, nsDOMGenericSH,
   1.436 +                                       DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.437 +
   1.438 +
   1.439 +  NS_DEFINE_CLASSINFO_DATA(MozCSSKeyframeRule, nsDOMGenericSH,
   1.440 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.441 +  NS_DEFINE_CLASSINFO_DATA(MozCSSKeyframesRule, nsDOMGenericSH,
   1.442 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.443 +
   1.444 +  NS_DEFINE_CLASSINFO_DATA(CSSPageRule, nsDOMGenericSH,
   1.445 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.446 +
   1.447 +  NS_DEFINE_CLASSINFO_DATA(CSSFontFeatureValuesRule, nsDOMGenericSH,
   1.448 +                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.449 +
   1.450 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(UserDataHandler, nsDOMGenericSH,
   1.451 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.452 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULControlElement, nsDOMGenericSH,
   1.453 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.454 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULLabeledControlElement, nsDOMGenericSH,
   1.455 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.456 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULButtonElement, nsDOMGenericSH,
   1.457 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.458 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULCheckboxElement, nsDOMGenericSH,
   1.459 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.460 +  NS_DEFINE_CHROME_XBL_CLASSINFO_DATA(XULPopupElement, nsDOMGenericSH,
   1.461 +                                      DOM_DEFAULT_SCRIPTABLE_FLAGS)
   1.462 +};
   1.463 +
   1.464 +#define NS_DEFINE_CONTRACT_CTOR(_class, _contract_id)                           \
   1.465 +  static nsresult                                                               \
   1.466 +  _class##Ctor(nsISupports** aInstancePtrResult)                                \
   1.467 +  {                                                                             \
   1.468 +    nsresult rv = NS_OK;                                                        \
   1.469 +    nsCOMPtr<nsISupports> native = do_CreateInstance(_contract_id, &rv);        \
   1.470 +    native.forget(aInstancePtrResult);                                          \
   1.471 +    return rv;                                                                  \
   1.472 +  }
   1.473 +
   1.474 +NS_DEFINE_CONTRACT_CTOR(XSLTProcessor,
   1.475 +                        "@mozilla.org/document-transformer;1?type=xslt")
   1.476 +
   1.477 +#undef NS_DEFINE_CONTRACT_CTOR
   1.478 +
   1.479 +struct nsConstructorFuncMapData
   1.480 +{
   1.481 +  int32_t mDOMClassInfoID;
   1.482 +  nsDOMConstructorFunc mConstructorFunc;
   1.483 +};
   1.484 +
   1.485 +#define NS_DEFINE_CONSTRUCTOR_FUNC_DATA(_class, _func)                        \
   1.486 +  { eDOMClassInfo_##_class##_id, _func },
   1.487 +
   1.488 +static const nsConstructorFuncMapData kConstructorFuncMap[] =
   1.489 +{
   1.490 +  NS_DEFINE_CONSTRUCTOR_FUNC_DATA(Blob, nsDOMMultipartFile::NewBlob)
   1.491 +  NS_DEFINE_CONSTRUCTOR_FUNC_DATA(File, nsDOMMultipartFile::NewFile)
   1.492 +  NS_DEFINE_CONSTRUCTOR_FUNC_DATA(MozSmsFilter, SmsFilter::NewSmsFilter)
   1.493 +  NS_DEFINE_CONSTRUCTOR_FUNC_DATA(XSLTProcessor, XSLTProcessorCtor)
   1.494 +};
   1.495 +#undef NS_DEFINE_CONSTRUCTOR_FUNC_DATA
   1.496 +
   1.497 +nsIXPConnect *nsDOMClassInfo::sXPConnect = nullptr;
   1.498 +nsIScriptSecurityManager *nsDOMClassInfo::sSecMan = nullptr;
   1.499 +bool nsDOMClassInfo::sIsInitialized = false;
   1.500 +
   1.501 +
   1.502 +jsid nsDOMClassInfo::sLocation_id        = JSID_VOID;
   1.503 +jsid nsDOMClassInfo::sConstructor_id     = JSID_VOID;
   1.504 +jsid nsDOMClassInfo::sLength_id          = JSID_VOID;
   1.505 +jsid nsDOMClassInfo::sItem_id            = JSID_VOID;
   1.506 +jsid nsDOMClassInfo::sNamedItem_id       = JSID_VOID;
   1.507 +jsid nsDOMClassInfo::sEnumerate_id       = JSID_VOID;
   1.508 +jsid nsDOMClassInfo::sTop_id             = JSID_VOID;
   1.509 +jsid nsDOMClassInfo::sDocument_id        = JSID_VOID;
   1.510 +jsid nsDOMClassInfo::sWrappedJSObject_id = JSID_VOID;
   1.511 +
   1.512 +static const JSClass *sObjectClass = nullptr;
   1.513 +
   1.514 +/**
   1.515 + * Set our JSClass pointer for the Object class
   1.516 + */
   1.517 +static void
   1.518 +FindObjectClass(JSContext* cx, JSObject* aGlobalObject)
   1.519 +{
   1.520 +  NS_ASSERTION(!sObjectClass,
   1.521 +               "Double set of sObjectClass");
   1.522 +  JS::Rooted<JSObject*> obj(cx), proto(cx, aGlobalObject);
   1.523 +  do {
   1.524 +    obj = proto;
   1.525 +    js::GetObjectProto(cx, obj, &proto);
   1.526 +  } while (proto);
   1.527 +
   1.528 +  sObjectClass = js::GetObjectJSClass(obj);
   1.529 +}
   1.530 +
   1.531 +static inline JSString *
   1.532 +IdToString(JSContext *cx, jsid id)
   1.533 +{
   1.534 +  if (JSID_IS_STRING(id))
   1.535 +    return JSID_TO_STRING(id);
   1.536 +  JS::Rooted<JS::Value> idval(cx);
   1.537 +  if (!::JS_IdToValue(cx, id, &idval))
   1.538 +    return nullptr;
   1.539 +  return JS::ToString(cx, idval);
   1.540 +}
   1.541 +
   1.542 +static inline nsresult
   1.543 +WrapNative(JSContext *cx, nsISupports *native,
   1.544 +           nsWrapperCache *cache, const nsIID* aIID, JS::MutableHandle<JS::Value> vp,
   1.545 +           bool aAllowWrapping)
   1.546 +{
   1.547 +  if (!native) {
   1.548 +    vp.setNull();
   1.549 +
   1.550 +    return NS_OK;
   1.551 +  }
   1.552 +
   1.553 +  JSObject *wrapper = xpc_FastGetCachedWrapper(cx, cache, vp);
   1.554 +  if (wrapper) {
   1.555 +    return NS_OK;
   1.556 +  }
   1.557 +
   1.558 +  JS::Rooted<JSObject*> scope(cx, JS::CurrentGlobalOrNull(cx));
   1.559 +  return nsDOMClassInfo::XPConnect()->WrapNativeToJSVal(cx, scope, native,
   1.560 +                                                        cache, aIID,
   1.561 +                                                        aAllowWrapping, vp);
   1.562 +}
   1.563 +
   1.564 +static inline nsresult
   1.565 +WrapNative(JSContext *cx, nsISupports *native, const nsIID* aIID,
   1.566 +           bool aAllowWrapping, JS::MutableHandle<JS::Value> vp)
   1.567 +{
   1.568 +  return WrapNative(cx, native, nullptr, aIID, vp, aAllowWrapping);
   1.569 +}
   1.570 +
   1.571 +// Same as the WrapNative above, but use these if aIID is nsISupports' IID.
   1.572 +static inline nsresult
   1.573 +WrapNative(JSContext *cx, nsISupports *native,
   1.574 +           bool aAllowWrapping, JS::MutableHandle<JS::Value> vp)
   1.575 +{
   1.576 +  return WrapNative(cx, native, nullptr, nullptr, vp, aAllowWrapping);
   1.577 +}
   1.578 +
   1.579 +static inline nsresult
   1.580 +WrapNative(JSContext *cx, nsISupports *native,
   1.581 +           nsWrapperCache *cache, bool aAllowWrapping,
   1.582 +           JS::MutableHandle<JS::Value> vp)
   1.583 +{
   1.584 +  return WrapNative(cx, native, cache, nullptr, vp, aAllowWrapping);
   1.585 +}
   1.586 +
   1.587 +// Helper to handle torn-down inner windows.
   1.588 +static inline nsresult
   1.589 +SetParentToWindow(nsGlobalWindow *win, JSObject **parent)
   1.590 +{
   1.591 +  MOZ_ASSERT(win);
   1.592 +  MOZ_ASSERT(win->IsInnerWindow());
   1.593 +  *parent = win->FastGetGlobalJSObject();
   1.594 +
   1.595 +  if (MOZ_UNLIKELY(!*parent)) {
   1.596 +    // The inner window has been torn down. The scope is dying, so don't create
   1.597 +    // any new wrappers.
   1.598 +    return NS_ERROR_FAILURE;
   1.599 +  }
   1.600 +  return NS_OK;
   1.601 +}
   1.602 +
   1.603 +// static
   1.604 +
   1.605 +nsISupports *
   1.606 +nsDOMClassInfo::GetNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj)
   1.607 +{
   1.608 +  return wrapper ? wrapper->Native() : static_cast<nsISupports*>(js::GetObjectPrivate(obj));
   1.609 +}
   1.610 +
   1.611 +nsresult
   1.612 +nsDOMClassInfo::DefineStaticJSVals(JSContext *cx)
   1.613 +{
   1.614 +#define SET_JSID_TO_STRING(_id, _cx, _str)                                    \
   1.615 +  if (JSString *str = ::JS_InternString(_cx, _str))                           \
   1.616 +      _id = INTERNED_STRING_TO_JSID(_cx, str);                                \
   1.617 +  else                                                                        \
   1.618 +      return NS_ERROR_OUT_OF_MEMORY;
   1.619 +
   1.620 +  SET_JSID_TO_STRING(sLocation_id,        cx, "location");
   1.621 +  SET_JSID_TO_STRING(sConstructor_id,     cx, "constructor");
   1.622 +  SET_JSID_TO_STRING(sLength_id,          cx, "length");
   1.623 +  SET_JSID_TO_STRING(sItem_id,            cx, "item");
   1.624 +  SET_JSID_TO_STRING(sNamedItem_id,       cx, "namedItem");
   1.625 +  SET_JSID_TO_STRING(sEnumerate_id,       cx, "enumerateProperties");
   1.626 +  SET_JSID_TO_STRING(sTop_id,             cx, "top");
   1.627 +  SET_JSID_TO_STRING(sDocument_id,        cx, "document");
   1.628 +  SET_JSID_TO_STRING(sWrappedJSObject_id, cx, "wrappedJSObject");
   1.629 +
   1.630 +  return NS_OK;
   1.631 +}
   1.632 +
   1.633 +// static
   1.634 +bool
   1.635 +nsDOMClassInfo::ObjectIsNativeWrapper(JSContext* cx, JSObject* obj)
   1.636 +{
   1.637 +  return xpc::WrapperFactory::IsXrayWrapper(obj) &&
   1.638 +         xpc::AccessCheck::wrapperSubsumes(obj);
   1.639 +}
   1.640 +
   1.641 +nsDOMClassInfo::nsDOMClassInfo(nsDOMClassInfoData* aData) : mData(aData)
   1.642 +{
   1.643 +}
   1.644 +
   1.645 +nsDOMClassInfo::~nsDOMClassInfo()
   1.646 +{
   1.647 +  if (IS_EXTERNAL(mData->mCachedClassInfo)) {
   1.648 +    // Some compilers don't like delete'ing a const nsDOMClassInfo*
   1.649 +    nsDOMClassInfoData* data = const_cast<nsDOMClassInfoData*>(mData);
   1.650 +    delete static_cast<nsExternalDOMClassInfoData*>(data);
   1.651 +  }
   1.652 +}
   1.653 +
   1.654 +NS_IMPL_ADDREF(nsDOMClassInfo)
   1.655 +NS_IMPL_RELEASE(nsDOMClassInfo)
   1.656 +
   1.657 +NS_INTERFACE_MAP_BEGIN(nsDOMClassInfo)
   1.658 +  if (aIID.Equals(NS_GET_IID(nsXPCClassInfo)))
   1.659 +    foundInterface = static_cast<nsIClassInfo*>(
   1.660 +                                    static_cast<nsXPCClassInfo*>(this));
   1.661 +  else
   1.662 +  NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
   1.663 +  NS_INTERFACE_MAP_ENTRY(nsIClassInfo)
   1.664 +  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIClassInfo)
   1.665 +NS_INTERFACE_MAP_END
   1.666 +
   1.667 +
   1.668 +static const JSClass sDOMConstructorProtoClass = {
   1.669 +  "DOM Constructor.prototype", 0,
   1.670 +  JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
   1.671 +  JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, nullptr
   1.672 +};
   1.673 +
   1.674 +
   1.675 +static const char *
   1.676 +CutPrefix(const char *aName) {
   1.677 +  static const char prefix_nsIDOM[] = "nsIDOM";
   1.678 +  static const char prefix_nsI[]    = "nsI";
   1.679 +
   1.680 +  if (strncmp(aName, prefix_nsIDOM, sizeof(prefix_nsIDOM) - 1) == 0) {
   1.681 +    return aName + sizeof(prefix_nsIDOM) - 1;
   1.682 +  }
   1.683 +
   1.684 +  if (strncmp(aName, prefix_nsI, sizeof(prefix_nsI) - 1) == 0) {
   1.685 +    return aName + sizeof(prefix_nsI) - 1;
   1.686 +  }
   1.687 +
   1.688 +  return aName;
   1.689 +}
   1.690 +
   1.691 +// static
   1.692 +nsresult
   1.693 +nsDOMClassInfo::RegisterClassProtos(int32_t aClassInfoID)
   1.694 +{
   1.695 +  nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
   1.696 +  NS_ENSURE_TRUE(nameSpaceManager, NS_ERROR_NOT_INITIALIZED);
   1.697 +  bool found_old;
   1.698 +
   1.699 +  const nsIID *primary_iid = sClassInfoData[aClassInfoID].mProtoChainInterface;
   1.700 +
   1.701 +  if (!primary_iid || primary_iid == &NS_GET_IID(nsISupports)) {
   1.702 +    return NS_OK;
   1.703 +  }
   1.704 +
   1.705 +  nsCOMPtr<nsIInterfaceInfoManager>
   1.706 +    iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
   1.707 +  NS_ENSURE_TRUE(iim, NS_ERROR_NOT_AVAILABLE);
   1.708 +
   1.709 +  nsCOMPtr<nsIInterfaceInfo> if_info;
   1.710 +  bool first = true;
   1.711 +
   1.712 +  iim->GetInfoForIID(primary_iid, getter_AddRefs(if_info));
   1.713 +
   1.714 +  while (if_info) {
   1.715 +    const nsIID *iid = nullptr;
   1.716 +
   1.717 +    if_info->GetIIDShared(&iid);
   1.718 +    NS_ENSURE_TRUE(iid, NS_ERROR_UNEXPECTED);
   1.719 +
   1.720 +    if (iid->Equals(NS_GET_IID(nsISupports))) {
   1.721 +      break;
   1.722 +    }
   1.723 +
   1.724 +    const char *name = nullptr;
   1.725 +    if_info->GetNameShared(&name);
   1.726 +    NS_ENSURE_TRUE(name, NS_ERROR_UNEXPECTED);
   1.727 +
   1.728 +    nameSpaceManager->RegisterClassProto(CutPrefix(name), iid, &found_old);
   1.729 +
   1.730 +    if (first) {
   1.731 +      first = false;
   1.732 +    } else if (found_old) {
   1.733 +      break;
   1.734 +    }
   1.735 +
   1.736 +    nsCOMPtr<nsIInterfaceInfo> tmp(if_info);
   1.737 +    tmp->GetParent(getter_AddRefs(if_info));
   1.738 +  }
   1.739 +
   1.740 +  return NS_OK;
   1.741 +}
   1.742 +
   1.743 +// static
   1.744 +nsresult
   1.745 +nsDOMClassInfo::RegisterExternalClasses()
   1.746 +{
   1.747 +  nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
   1.748 +  NS_ENSURE_TRUE(nameSpaceManager, NS_ERROR_NOT_INITIALIZED);
   1.749 +
   1.750 +  nsCOMPtr<nsIComponentRegistrar> registrar;
   1.751 +  nsresult rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
   1.752 +  NS_ENSURE_SUCCESS(rv, rv);
   1.753 +
   1.754 +  nsCOMPtr<nsICategoryManager> cm =
   1.755 +    do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
   1.756 +  NS_ENSURE_SUCCESS(rv, rv);
   1.757 +
   1.758 +  nsCOMPtr<nsISimpleEnumerator> e;
   1.759 +  rv = cm->EnumerateCategory(JAVASCRIPT_DOM_CLASS, getter_AddRefs(e));
   1.760 +  NS_ENSURE_SUCCESS(rv, rv);
   1.761 +
   1.762 +  nsXPIDLCString contractId;
   1.763 +  nsAutoCString categoryEntry;
   1.764 +  nsCOMPtr<nsISupports> entry;
   1.765 +
   1.766 +  while (NS_SUCCEEDED(e->GetNext(getter_AddRefs(entry)))) {
   1.767 +    nsCOMPtr<nsISupportsCString> category(do_QueryInterface(entry));
   1.768 +
   1.769 +    if (!category) {
   1.770 +      NS_WARNING("Category entry not an nsISupportsCString!");
   1.771 +      continue;
   1.772 +    }
   1.773 +
   1.774 +    rv = category->GetData(categoryEntry);
   1.775 +
   1.776 +    cm->GetCategoryEntry(JAVASCRIPT_DOM_CLASS, categoryEntry.get(),
   1.777 +                         getter_Copies(contractId));
   1.778 +    NS_ENSURE_SUCCESS(rv, rv);
   1.779 +
   1.780 +    nsCID *cid;
   1.781 +    rv = registrar->ContractIDToCID(contractId, &cid);
   1.782 +    if (NS_FAILED(rv)) {
   1.783 +      NS_WARNING("Bad contract id registered with the script namespace manager");
   1.784 +      continue;
   1.785 +    }
   1.786 +
   1.787 +    rv = nameSpaceManager->RegisterExternalClassName(categoryEntry.get(), *cid);
   1.788 +    nsMemory::Free(cid);
   1.789 +    NS_ENSURE_SUCCESS(rv, rv);
   1.790 +  }
   1.791 +
   1.792 +  return nameSpaceManager->RegisterExternalInterfaces(true);
   1.793 +}
   1.794 +
   1.795 +#define _DOM_CLASSINFO_MAP_BEGIN(_class, _ifptr, _has_class_if)               \
   1.796 +  {                                                                           \
   1.797 +    nsDOMClassInfoData &d = sClassInfoData[eDOMClassInfo_##_class##_id];      \
   1.798 +    d.mProtoChainInterface = _ifptr;                                          \
   1.799 +    d.mHasClassInterface = _has_class_if;                                     \
   1.800 +    d.mInterfacesBitmap = kDOMClassInfo_##_class##_interfaces;                \
   1.801 +    static const nsIID *interface_list[] = {
   1.802 +
   1.803 +#define DOM_CLASSINFO_MAP_BEGIN(_class, _interface)                           \
   1.804 +  _DOM_CLASSINFO_MAP_BEGIN(_class, &NS_GET_IID(_interface), true)
   1.805 +
   1.806 +#define DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(_class, _interface)               \
   1.807 +  _DOM_CLASSINFO_MAP_BEGIN(_class, &NS_GET_IID(_interface), false)
   1.808 +
   1.809 +#define DOM_CLASSINFO_MAP_ENTRY(_if)                                          \
   1.810 +      &NS_GET_IID(_if),
   1.811 +
   1.812 +#define DOM_CLASSINFO_MAP_CONDITIONAL_ENTRY(_if, _cond)                       \
   1.813 +      (_cond) ? &NS_GET_IID(_if) : nullptr,
   1.814 +
   1.815 +#define DOM_CLASSINFO_MAP_END                                                 \
   1.816 +      nullptr                                                                  \
   1.817 +    };                                                                        \
   1.818 +                                                                              \
   1.819 +    /* Compact the interface list */                                          \
   1.820 +    size_t count = ArrayLength(interface_list);                               \
   1.821 +    /* count is the number of array entries, which is one greater than the */ \
   1.822 +    /* number of interfaces due to the terminating null */                    \
   1.823 +    for (size_t i = 0; i < count - 1; ++i) {                                  \
   1.824 +      if (!interface_list[i]) {                                               \
   1.825 +        /* We are moving the element at index i+1 and successors, */          \
   1.826 +        /* so we must move only count - (i+1) elements total. */              \
   1.827 +        memmove(&interface_list[i], &interface_list[i+1],                     \
   1.828 +                sizeof(nsIID*) * (count - (i+1)));                            \
   1.829 +        /* Make sure to examine the new pointer we ended up with at this */   \
   1.830 +        /* slot, since it may be null too */                                  \
   1.831 +        --i;                                                                  \
   1.832 +        --count;                                                              \
   1.833 +      }                                                                       \
   1.834 +    }                                                                         \
   1.835 +                                                                              \
   1.836 +    d.mInterfaces = interface_list;                                           \
   1.837 +  }
   1.838 +
   1.839 +#ifdef MOZ_B2G
   1.840 +#define DOM_CLASSINFO_WINDOW_MAP_ENTRIES                                       \
   1.841 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindow)                                        \
   1.842 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindowB2G)                                     \
   1.843 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMJSWindow)                                      \
   1.844 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)                                   \
   1.845 +  DOM_CLASSINFO_MAP_ENTRY(nsIInlineEventHandlers)                              \
   1.846 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindowPerformance)                             \
   1.847 +  DOM_CLASSINFO_MAP_ENTRY(nsIInterfaceRequestor)                               \
   1.848 +  DOM_CLASSINFO_MAP_CONDITIONAL_ENTRY(nsITouchEventReceiver,                   \
   1.849 +                                      TouchEvent::PrefEnabled())
   1.850 +#else // !MOZ_B2G
   1.851 +#define DOM_CLASSINFO_WINDOW_MAP_ENTRIES                                       \
   1.852 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindow)                                        \
   1.853 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMJSWindow)                                      \
   1.854 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)                                   \
   1.855 +  DOM_CLASSINFO_MAP_ENTRY(nsIInlineEventHandlers)                              \
   1.856 +  DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindowPerformance)                             \
   1.857 +  DOM_CLASSINFO_MAP_ENTRY(nsIInterfaceRequestor)                               \
   1.858 +  DOM_CLASSINFO_MAP_CONDITIONAL_ENTRY(nsITouchEventReceiver,                   \
   1.859 +                                      TouchEvent::PrefEnabled())
   1.860 +#endif // MOZ_B2G
   1.861 +
   1.862 +nsresult
   1.863 +nsDOMClassInfo::Init()
   1.864 +{
   1.865 +  /* Errors that can trigger early returns are done first,
   1.866 +     otherwise nsDOMClassInfo is left in a half inited state. */
   1.867 +  static_assert(sizeof(uintptr_t) == sizeof(void*),
   1.868 +                "BAD! You'll need to adjust the size of uintptr_t to the "
   1.869 +                "size of a pointer on your platform.");
   1.870 +
   1.871 +  NS_ENSURE_TRUE(!sIsInitialized, NS_ERROR_ALREADY_INITIALIZED);
   1.872 +
   1.873 +  nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
   1.874 +  NS_ENSURE_TRUE(nameSpaceManager, NS_ERROR_NOT_INITIALIZED);
   1.875 +
   1.876 +  nsresult rv = CallGetService(nsIXPConnect::GetCID(), &sXPConnect);
   1.877 +  NS_ENSURE_SUCCESS(rv, rv);
   1.878 +
   1.879 +  nsCOMPtr<nsIXPCFunctionThisTranslator> elt = new nsEventListenerThisTranslator();
   1.880 +  sXPConnect->SetFunctionThisTranslator(NS_GET_IID(nsIDOMEventListener), elt);
   1.881 +
   1.882 +  nsCOMPtr<nsIScriptSecurityManager> sm =
   1.883 +    do_GetService("@mozilla.org/scriptsecuritymanager;1", &rv);
   1.884 +  NS_ENSURE_SUCCESS(rv, rv);
   1.885 +
   1.886 +  sSecMan = sm;
   1.887 +  NS_ADDREF(sSecMan);
   1.888 +
   1.889 +  AutoSafeJSContext cx;
   1.890 +
   1.891 +  DOM_CLASSINFO_MAP_BEGIN(Window, nsIDOMWindow)
   1.892 +    DOM_CLASSINFO_WINDOW_MAP_ENTRIES
   1.893 +#ifdef MOZ_WEBSPEECH
   1.894 +    DOM_CLASSINFO_MAP_ENTRY(nsISpeechSynthesisGetter)
   1.895 +#endif
   1.896 +  DOM_CLASSINFO_MAP_END
   1.897 +
   1.898 +  DOM_CLASSINFO_MAP_BEGIN(WindowUtils, nsIDOMWindowUtils)
   1.899 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindowUtils)
   1.900 +  DOM_CLASSINFO_MAP_END
   1.901 +
   1.902 +  DOM_CLASSINFO_MAP_BEGIN(Location, nsIDOMLocation)
   1.903 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMLocation)
   1.904 +  DOM_CLASSINFO_MAP_END
   1.905 +
   1.906 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(DOMPrototype, nsIDOMDOMConstructor)
   1.907 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMDOMConstructor)
   1.908 +  DOM_CLASSINFO_MAP_END
   1.909 +
   1.910 +  DOM_CLASSINFO_MAP_BEGIN(DOMConstructor, nsIDOMDOMConstructor)
   1.911 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMDOMConstructor)
   1.912 +  DOM_CLASSINFO_MAP_END
   1.913 +
   1.914 +  DOM_CLASSINFO_MAP_BEGIN(CSSStyleRule, nsIDOMCSSStyleRule)
   1.915 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSStyleRule)
   1.916 +  DOM_CLASSINFO_MAP_END
   1.917 +
   1.918 +  DOM_CLASSINFO_MAP_BEGIN(CSSCharsetRule, nsIDOMCSSCharsetRule)
   1.919 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSCharsetRule)
   1.920 +  DOM_CLASSINFO_MAP_END
   1.921 +
   1.922 +  DOM_CLASSINFO_MAP_BEGIN(CSSImportRule, nsIDOMCSSImportRule)
   1.923 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSImportRule)
   1.924 +  DOM_CLASSINFO_MAP_END
   1.925 +
   1.926 +  DOM_CLASSINFO_MAP_BEGIN(CSSMediaRule, nsIDOMCSSMediaRule)
   1.927 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSMediaRule)
   1.928 +  DOM_CLASSINFO_MAP_END
   1.929 +
   1.930 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(CSSNameSpaceRule, nsIDOMCSSRule)
   1.931 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSRule)
   1.932 +  DOM_CLASSINFO_MAP_END
   1.933 +
   1.934 +  DOM_CLASSINFO_MAP_BEGIN(CSSRuleList, nsIDOMCSSRuleList)
   1.935 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSRuleList)
   1.936 +  DOM_CLASSINFO_MAP_END
   1.937 +
   1.938 +  DOM_CLASSINFO_MAP_BEGIN(CSSStyleSheet, nsIDOMCSSStyleSheet)
   1.939 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSStyleSheet)
   1.940 +  DOM_CLASSINFO_MAP_END
   1.941 +
   1.942 +#ifdef MOZ_XUL
   1.943 +  DOM_CLASSINFO_MAP_BEGIN(XULCommandDispatcher, nsIDOMXULCommandDispatcher)
   1.944 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXULCommandDispatcher)
   1.945 +  DOM_CLASSINFO_MAP_END
   1.946 +#endif
   1.947 +
   1.948 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(XULControllers, nsIControllers)
   1.949 +    DOM_CLASSINFO_MAP_ENTRY(nsIControllers)
   1.950 +  DOM_CLASSINFO_MAP_END
   1.951 +
   1.952 +  DOM_CLASSINFO_MAP_BEGIN(BoxObject, nsIBoxObject)
   1.953 +    DOM_CLASSINFO_MAP_ENTRY(nsIBoxObject)
   1.954 +  DOM_CLASSINFO_MAP_END
   1.955 +
   1.956 +#ifdef MOZ_XUL
   1.957 +  DOM_CLASSINFO_MAP_BEGIN(TreeSelection, nsITreeSelection)
   1.958 +    DOM_CLASSINFO_MAP_ENTRY(nsITreeSelection)
   1.959 +  DOM_CLASSINFO_MAP_END
   1.960 +
   1.961 +  DOM_CLASSINFO_MAP_BEGIN(TreeContentView, nsITreeContentView)
   1.962 +    DOM_CLASSINFO_MAP_ENTRY(nsITreeContentView)
   1.963 +    DOM_CLASSINFO_MAP_ENTRY(nsITreeView)
   1.964 +  DOM_CLASSINFO_MAP_END
   1.965 +#endif
   1.966 +
   1.967 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(ChromeWindow, nsIDOMWindow)
   1.968 +    DOM_CLASSINFO_WINDOW_MAP_ENTRIES
   1.969 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMChromeWindow)
   1.970 +#ifdef MOZ_WEBSPEECH
   1.971 +    DOM_CLASSINFO_MAP_ENTRY(nsISpeechSynthesisGetter)
   1.972 +#endif
   1.973 +  DOM_CLASSINFO_MAP_END
   1.974 +
   1.975 +#ifdef MOZ_XUL
   1.976 +  DOM_CLASSINFO_MAP_BEGIN(XULTemplateBuilder, nsIXULTemplateBuilder)
   1.977 +    DOM_CLASSINFO_MAP_ENTRY(nsIXULTemplateBuilder)
   1.978 +  DOM_CLASSINFO_MAP_END
   1.979 +
   1.980 +  DOM_CLASSINFO_MAP_BEGIN(XULTreeBuilder, nsIXULTreeBuilder)
   1.981 +    DOM_CLASSINFO_MAP_ENTRY(nsIXULTreeBuilder)
   1.982 +    DOM_CLASSINFO_MAP_ENTRY(nsIXULTemplateBuilder)
   1.983 +    DOM_CLASSINFO_MAP_ENTRY(nsITreeView)
   1.984 +  DOM_CLASSINFO_MAP_END
   1.985 +#endif
   1.986 +
   1.987 +#ifdef MOZ_XUL
   1.988 +  DOM_CLASSINFO_MAP_BEGIN(TreeColumn, nsITreeColumn)
   1.989 +    DOM_CLASSINFO_MAP_ENTRY(nsITreeColumn)
   1.990 +  DOM_CLASSINFO_MAP_END
   1.991 +#endif
   1.992 +
   1.993 +  DOM_CLASSINFO_MAP_BEGIN(CSSMozDocumentRule, nsIDOMCSSMozDocumentRule)
   1.994 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSMozDocumentRule)
   1.995 +  DOM_CLASSINFO_MAP_END
   1.996 +
   1.997 +  DOM_CLASSINFO_MAP_BEGIN(CSSSupportsRule, nsIDOMCSSSupportsRule)
   1.998 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSSupportsRule)
   1.999 +  DOM_CLASSINFO_MAP_END
  1.1000 +
  1.1001 +  // The SVG document
  1.1002 +
  1.1003 +  // other SVG classes
  1.1004 +  DOM_CLASSINFO_MAP_BEGIN(SVGNumber, nsIDOMSVGNumber)
  1.1005 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMSVGNumber)
  1.1006 +  DOM_CLASSINFO_MAP_END
  1.1007 +
  1.1008 +  DOM_CLASSINFO_MAP_BEGIN(XSLTProcessor, nsIXSLTProcessor)
  1.1009 +    DOM_CLASSINFO_MAP_ENTRY(nsIXSLTProcessor)
  1.1010 +    DOM_CLASSINFO_MAP_ENTRY(nsIXSLTProcessorPrivate)
  1.1011 +  DOM_CLASSINFO_MAP_END
  1.1012 +
  1.1013 +  DOM_CLASSINFO_MAP_BEGIN(XPathExpression, nsIDOMXPathExpression)
  1.1014 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXPathExpression)
  1.1015 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMNSXPathExpression)
  1.1016 +  DOM_CLASSINFO_MAP_END
  1.1017 +
  1.1018 +  DOM_CLASSINFO_MAP_BEGIN(XPathNSResolver, nsIDOMXPathNSResolver)
  1.1019 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXPathNSResolver)
  1.1020 +  DOM_CLASSINFO_MAP_END
  1.1021 +
  1.1022 +  DOM_CLASSINFO_MAP_BEGIN(XPathResult, nsIDOMXPathResult)
  1.1023 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXPathResult)
  1.1024 +  DOM_CLASSINFO_MAP_END
  1.1025 +
  1.1026 +  DOM_CLASSINFO_MAP_BEGIN(Storage, nsIDOMStorage)
  1.1027 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMStorage)
  1.1028 +  DOM_CLASSINFO_MAP_END
  1.1029 +
  1.1030 +  DOM_CLASSINFO_MAP_BEGIN(Blob, nsIDOMBlob)
  1.1031 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMBlob)
  1.1032 +  DOM_CLASSINFO_MAP_END
  1.1033 +
  1.1034 +  DOM_CLASSINFO_MAP_BEGIN(File, nsIDOMFile)
  1.1035 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMBlob)
  1.1036 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMFile)
  1.1037 +  DOM_CLASSINFO_MAP_END
  1.1038 +
  1.1039 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(ModalContentWindow, nsIDOMWindow)
  1.1040 +    DOM_CLASSINFO_WINDOW_MAP_ENTRIES
  1.1041 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMModalContentWindow)
  1.1042 +#ifdef MOZ_WEBSPEECH
  1.1043 +    DOM_CLASSINFO_MAP_ENTRY(nsISpeechSynthesisGetter)
  1.1044 +#endif
  1.1045 +  DOM_CLASSINFO_MAP_END
  1.1046 +
  1.1047 +  DOM_CLASSINFO_MAP_BEGIN(MozMobileMessageManager, nsIDOMMozMobileMessageManager)
  1.1048 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozMobileMessageManager)
  1.1049 +  DOM_CLASSINFO_MAP_END
  1.1050 +
  1.1051 +  DOM_CLASSINFO_MAP_BEGIN(MozSmsMessage, nsIDOMMozSmsMessage)
  1.1052 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozSmsMessage)
  1.1053 +  DOM_CLASSINFO_MAP_END
  1.1054 +
  1.1055 +  DOM_CLASSINFO_MAP_BEGIN(MozMmsMessage, nsIDOMMozMmsMessage)
  1.1056 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozMmsMessage)
  1.1057 +  DOM_CLASSINFO_MAP_END
  1.1058 +
  1.1059 +  DOM_CLASSINFO_MAP_BEGIN(MozSmsFilter, nsIDOMMozSmsFilter)
  1.1060 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozSmsFilter)
  1.1061 +  DOM_CLASSINFO_MAP_END
  1.1062 +
  1.1063 +  DOM_CLASSINFO_MAP_BEGIN(MozSmsSegmentInfo, nsIDOMMozSmsSegmentInfo)
  1.1064 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozSmsSegmentInfo)
  1.1065 +  DOM_CLASSINFO_MAP_END
  1.1066 +
  1.1067 +  DOM_CLASSINFO_MAP_BEGIN(MozMobileMessageThread, nsIDOMMozMobileMessageThread)
  1.1068 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozMobileMessageThread)
  1.1069 +  DOM_CLASSINFO_MAP_END
  1.1070 +
  1.1071 +#ifdef MOZ_B2G_RIL
  1.1072 +  DOM_CLASSINFO_MAP_BEGIN(MozMobileConnection, nsIDOMMozMobileConnection)
  1.1073 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozMobileConnection)
  1.1074 +     DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
  1.1075 +  DOM_CLASSINFO_MAP_END
  1.1076 +#endif // MOZ_B2G_RIL
  1.1077 +
  1.1078 +  DOM_CLASSINFO_MAP_BEGIN(CSSFontFaceRule, nsIDOMCSSFontFaceRule)
  1.1079 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFaceRule)
  1.1080 +  DOM_CLASSINFO_MAP_END
  1.1081 +
  1.1082 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(ContentFrameMessageManager, nsISupports)
  1.1083 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
  1.1084 +    DOM_CLASSINFO_MAP_ENTRY(nsIMessageListenerManager)
  1.1085 +    DOM_CLASSINFO_MAP_ENTRY(nsIMessageSender)
  1.1086 +    DOM_CLASSINFO_MAP_ENTRY(nsISyncMessageSender)
  1.1087 +    DOM_CLASSINFO_MAP_ENTRY(nsIContentFrameMessageManager)
  1.1088 +  DOM_CLASSINFO_MAP_END
  1.1089 +
  1.1090 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(ChromeMessageBroadcaster, nsISupports)
  1.1091 +    DOM_CLASSINFO_MAP_ENTRY(nsIFrameScriptLoader)
  1.1092 +    DOM_CLASSINFO_MAP_ENTRY(nsIMessageListenerManager)
  1.1093 +    DOM_CLASSINFO_MAP_ENTRY(nsIMessageBroadcaster)
  1.1094 +  DOM_CLASSINFO_MAP_END
  1.1095 +
  1.1096 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(ChromeMessageSender, nsISupports)
  1.1097 +    DOM_CLASSINFO_MAP_ENTRY(nsIProcessChecker)
  1.1098 +    DOM_CLASSINFO_MAP_ENTRY(nsIFrameScriptLoader)
  1.1099 +    DOM_CLASSINFO_MAP_ENTRY(nsIMessageListenerManager)
  1.1100 +    DOM_CLASSINFO_MAP_ENTRY(nsIMessageSender)
  1.1101 +  DOM_CLASSINFO_MAP_END
  1.1102 +
  1.1103 +  DOM_CLASSINFO_MAP_BEGIN(MozCSSKeyframeRule, nsIDOMMozCSSKeyframeRule)
  1.1104 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozCSSKeyframeRule)
  1.1105 +  DOM_CLASSINFO_MAP_END
  1.1106 +
  1.1107 +  DOM_CLASSINFO_MAP_BEGIN(MozCSSKeyframesRule, nsIDOMMozCSSKeyframesRule)
  1.1108 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozCSSKeyframesRule)
  1.1109 +  DOM_CLASSINFO_MAP_END
  1.1110 +
  1.1111 +  DOM_CLASSINFO_MAP_BEGIN(CSSPageRule, nsIDOMCSSPageRule)
  1.1112 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSPageRule)
  1.1113 +  DOM_CLASSINFO_MAP_END
  1.1114 +
  1.1115 +  DOM_CLASSINFO_MAP_BEGIN(CSSFontFeatureValuesRule, nsIDOMCSSFontFeatureValuesRule)
  1.1116 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFeatureValuesRule)
  1.1117 +  DOM_CLASSINFO_MAP_END
  1.1118 +
  1.1119 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(UserDataHandler, nsIDOMUserDataHandler)
  1.1120 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMUserDataHandler)
  1.1121 +  DOM_CLASSINFO_MAP_END
  1.1122 +
  1.1123 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(XULControlElement, nsIDOMXULControlElement)
  1.1124 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXULControlElement)
  1.1125 +  DOM_CLASSINFO_MAP_END
  1.1126 +
  1.1127 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(XULLabeledControlElement, nsIDOMXULLabeledControlElement)
  1.1128 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXULLabeledControlElement)
  1.1129 +  DOM_CLASSINFO_MAP_END
  1.1130 +
  1.1131 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(XULButtonElement, nsIDOMXULButtonElement)
  1.1132 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXULButtonElement)
  1.1133 +  DOM_CLASSINFO_MAP_END
  1.1134 +
  1.1135 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(XULCheckboxElement, nsIDOMXULCheckboxElement)
  1.1136 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXULCheckboxElement)
  1.1137 +  DOM_CLASSINFO_MAP_END
  1.1138 +
  1.1139 +  DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(XULPopupElement, nsIDOMXULPopupElement)
  1.1140 +    DOM_CLASSINFO_MAP_ENTRY(nsIDOMXULPopupElement)
  1.1141 +  DOM_CLASSINFO_MAP_END
  1.1142 +
  1.1143 +  static_assert(MOZ_ARRAY_LENGTH(sClassInfoData) == eDOMClassInfoIDCount,
  1.1144 +                "The number of items in sClassInfoData doesn't match the "
  1.1145 +                "number of nsIDOMClassInfo ID's, this is bad! Fix it!");
  1.1146 +
  1.1147 +#ifdef DEBUG
  1.1148 +  for (size_t i = 0; i < eDOMClassInfoIDCount; i++) {
  1.1149 +    if (!sClassInfoData[i].u.mConstructorFptr ||
  1.1150 +        sClassInfoData[i].mDebugID != i) {
  1.1151 +      MOZ_CRASH("Class info data out of sync, you forgot to update "
  1.1152 +                "nsDOMClassInfo.h and nsDOMClassInfo.cpp! Fix this, "
  1.1153 +                "mozilla will not work without this fixed!");
  1.1154 +    }
  1.1155 +  }
  1.1156 +
  1.1157 +  for (size_t i = 0; i < eDOMClassInfoIDCount; i++) {
  1.1158 +    if (!sClassInfoData[i].mInterfaces) {
  1.1159 +      MOZ_CRASH("Class info data without an interface list! Fix this, "
  1.1160 +                "mozilla will not work without this fixed!");
  1.1161 +     }
  1.1162 +   }
  1.1163 +#endif
  1.1164 +
  1.1165 +  // Initialize static JSString's
  1.1166 +  DefineStaticJSVals(cx);
  1.1167 +
  1.1168 +  int32_t i;
  1.1169 +
  1.1170 +  for (i = 0; i < eDOMClassInfoIDCount; ++i) {
  1.1171 +    if (i == eDOMClassInfo_DOMPrototype_id) {
  1.1172 +      continue;
  1.1173 +    }
  1.1174 +
  1.1175 +    nsDOMClassInfoData& data = sClassInfoData[i];
  1.1176 +    nameSpaceManager->RegisterClassName(data.mName, i, data.mChromeOnly,
  1.1177 +                                        data.mAllowXBL, &data.mNameUTF16);
  1.1178 +  }
  1.1179 +
  1.1180 +  for (i = 0; i < eDOMClassInfoIDCount; ++i) {
  1.1181 +    RegisterClassProtos(i);
  1.1182 +  }
  1.1183 +
  1.1184 +  RegisterExternalClasses();
  1.1185 +
  1.1186 +  // Register new DOM bindings
  1.1187 +  mozilla::dom::Register(nameSpaceManager);
  1.1188 +
  1.1189 +  sIsInitialized = true;
  1.1190 +
  1.1191 +  return NS_OK;
  1.1192 +}
  1.1193 +
  1.1194 +// static
  1.1195 +int32_t
  1.1196 +nsDOMClassInfo::GetArrayIndexFromId(JSContext *cx, JS::Handle<jsid> id, bool *aIsNumber)
  1.1197 +{
  1.1198 +  if (aIsNumber) {
  1.1199 +    *aIsNumber = false;
  1.1200 +  }
  1.1201 +
  1.1202 +  int i;
  1.1203 +  if (JSID_IS_INT(id)) {
  1.1204 +      i = JSID_TO_INT(id);
  1.1205 +  } else {
  1.1206 +      JS::Rooted<JS::Value> idval(cx);
  1.1207 +      double array_index;
  1.1208 +      if (!::JS_IdToValue(cx, id, &idval) ||
  1.1209 +          !JS::ToNumber(cx, idval, &array_index) ||
  1.1210 +          !::JS_DoubleIsInt32(array_index, &i)) {
  1.1211 +        return -1;
  1.1212 +      }
  1.1213 +  }
  1.1214 +
  1.1215 +  if (aIsNumber) {
  1.1216 +    *aIsNumber = true;
  1.1217 +  }
  1.1218 +
  1.1219 +  return i;
  1.1220 +}
  1.1221 +
  1.1222 +NS_IMETHODIMP
  1.1223 +nsDOMClassInfo::GetInterfaces(uint32_t *aCount, nsIID ***aArray)
  1.1224 +{
  1.1225 +  uint32_t count = 0;
  1.1226 +
  1.1227 +  while (mData->mInterfaces[count]) {
  1.1228 +    count++;
  1.1229 +  }
  1.1230 +
  1.1231 +  *aCount = count;
  1.1232 +
  1.1233 +  if (!count) {
  1.1234 +    *aArray = nullptr;
  1.1235 +
  1.1236 +    return NS_OK;
  1.1237 +  }
  1.1238 +
  1.1239 +  *aArray = static_cast<nsIID **>(nsMemory::Alloc(count * sizeof(nsIID *)));
  1.1240 +  NS_ENSURE_TRUE(*aArray, NS_ERROR_OUT_OF_MEMORY);
  1.1241 +
  1.1242 +  uint32_t i;
  1.1243 +  for (i = 0; i < count; i++) {
  1.1244 +    nsIID *iid = static_cast<nsIID *>(nsMemory::Clone(mData->mInterfaces[i],
  1.1245 +                                                         sizeof(nsIID)));
  1.1246 +
  1.1247 +    if (!iid) {
  1.1248 +      NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(i, *aArray);
  1.1249 +
  1.1250 +      return NS_ERROR_OUT_OF_MEMORY;
  1.1251 +    }
  1.1252 +
  1.1253 +    *((*aArray) + i) = iid;
  1.1254 +  }
  1.1255 +
  1.1256 +  return NS_OK;
  1.1257 +}
  1.1258 +
  1.1259 +NS_IMETHODIMP
  1.1260 +nsDOMClassInfo::GetHelperForLanguage(uint32_t language, nsISupports **_retval)
  1.1261 +{
  1.1262 +  if (language == nsIProgrammingLanguage::JAVASCRIPT) {
  1.1263 +    *_retval = static_cast<nsIXPCScriptable *>(this);
  1.1264 +
  1.1265 +    NS_ADDREF(*_retval);
  1.1266 +  } else {
  1.1267 +    *_retval = nullptr;
  1.1268 +  }
  1.1269 +
  1.1270 +  return NS_OK;
  1.1271 +}
  1.1272 +
  1.1273 +NS_IMETHODIMP
  1.1274 +nsDOMClassInfo::GetContractID(char **aContractID)
  1.1275 +{
  1.1276 +  *aContractID = nullptr;
  1.1277 +
  1.1278 +  return NS_OK;
  1.1279 +}
  1.1280 +
  1.1281 +NS_IMETHODIMP
  1.1282 +nsDOMClassInfo::GetClassDescription(char **aClassDescription)
  1.1283 +{
  1.1284 +  return GetClassName(aClassDescription);
  1.1285 +}
  1.1286 +
  1.1287 +NS_IMETHODIMP
  1.1288 +nsDOMClassInfo::GetClassID(nsCID **aClassID)
  1.1289 +{
  1.1290 +  *aClassID = nullptr;
  1.1291 +  return NS_OK;
  1.1292 +}
  1.1293 +
  1.1294 +NS_IMETHODIMP
  1.1295 +nsDOMClassInfo::GetClassIDNoAlloc(nsCID *aClassID)
  1.1296 +{
  1.1297 +  return NS_ERROR_NOT_AVAILABLE;
  1.1298 +}
  1.1299 +
  1.1300 +NS_IMETHODIMP
  1.1301 +nsDOMClassInfo::GetImplementationLanguage(uint32_t *aImplLanguage)
  1.1302 +{
  1.1303 +  *aImplLanguage = nsIProgrammingLanguage::CPLUSPLUS;
  1.1304 +
  1.1305 +  return NS_OK;
  1.1306 +}
  1.1307 +
  1.1308 +NS_IMETHODIMP
  1.1309 +nsDOMClassInfo::GetFlags(uint32_t *aFlags)
  1.1310 +{
  1.1311 +  *aFlags = DOMCLASSINFO_STANDARD_FLAGS;
  1.1312 +
  1.1313 +  return NS_OK;
  1.1314 +}
  1.1315 +
  1.1316 +// nsIXPCScriptable
  1.1317 +
  1.1318 +NS_IMETHODIMP
  1.1319 +nsDOMClassInfo::GetClassName(char **aClassName)
  1.1320 +{
  1.1321 +  *aClassName = NS_strdup(mData->mName);
  1.1322 +
  1.1323 +  return NS_OK;
  1.1324 +}
  1.1325 +
  1.1326 +// virtual
  1.1327 +uint32_t
  1.1328 +nsDOMClassInfo::GetScriptableFlags()
  1.1329 +{
  1.1330 +  return mData->mScriptableFlags;
  1.1331 +}
  1.1332 +
  1.1333 +NS_IMETHODIMP
  1.1334 +nsDOMClassInfo::PreCreate(nsISupports *nativeObj, JSContext *cx,
  1.1335 +                          JSObject *globalObj, JSObject **parentObj)
  1.1336 +{
  1.1337 +  *parentObj = globalObj;
  1.1338 +  return NS_OK;
  1.1339 +}
  1.1340 +
  1.1341 +NS_IMETHODIMP
  1.1342 +nsDOMClassInfo::Create(nsIXPConnectWrappedNative *wrapper,
  1.1343 +                       JSContext *cx, JSObject *obj)
  1.1344 +{
  1.1345 +  NS_WARNING("nsDOMClassInfo::Create Don't call me!");
  1.1346 +
  1.1347 +  return NS_ERROR_UNEXPECTED;
  1.1348 +}
  1.1349 +
  1.1350 +NS_IMETHODIMP
  1.1351 +nsDOMClassInfo::PostCreate(nsIXPConnectWrappedNative *wrapper,
  1.1352 +                           JSContext *cx, JSObject *obj)
  1.1353 +{
  1.1354 +  NS_WARNING("nsDOMClassInfo::PostCreate Don't call me!");
  1.1355 +
  1.1356 +  return NS_ERROR_UNEXPECTED;
  1.1357 +}
  1.1358 +
  1.1359 +NS_IMETHODIMP
  1.1360 +nsDOMClassInfo::PostTransplant(nsIXPConnectWrappedNative *wrapper,
  1.1361 +                               JSContext *cx, JSObject *obj)
  1.1362 +{
  1.1363 +  MOZ_CRASH("nsDOMClassInfo::PostTransplant Don't call me!");
  1.1364 +}
  1.1365 +
  1.1366 +NS_IMETHODIMP
  1.1367 +nsDOMClassInfo::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1368 +                            JSObject *obj, jsid id, jsval *vp,
  1.1369 +                            bool *_retval)
  1.1370 +{
  1.1371 +  NS_WARNING("nsDOMClassInfo::AddProperty Don't call me!");
  1.1372 +
  1.1373 +  return NS_ERROR_UNEXPECTED;
  1.1374 +}
  1.1375 +
  1.1376 +NS_IMETHODIMP
  1.1377 +nsDOMClassInfo::DelProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1378 +                            JSObject *obj, jsid id, bool *_retval)
  1.1379 +{
  1.1380 +  NS_WARNING("nsDOMClassInfo::DelProperty Don't call me!");
  1.1381 +
  1.1382 +  return NS_ERROR_UNEXPECTED;
  1.1383 +}
  1.1384 +
  1.1385 +NS_IMETHODIMP
  1.1386 +nsDOMClassInfo::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1387 +                            JSObject *obj, jsid id, jsval *vp,
  1.1388 +                            bool *_retval)
  1.1389 +{
  1.1390 +  NS_WARNING("nsDOMClassInfo::GetProperty Don't call me!");
  1.1391 +
  1.1392 +  return NS_OK;
  1.1393 +}
  1.1394 +
  1.1395 +NS_IMETHODIMP
  1.1396 +nsDOMClassInfo::SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1397 +                            JSObject *obj, jsid id, jsval *vp,
  1.1398 +                            bool *_retval)
  1.1399 +{
  1.1400 +  NS_WARNING("nsDOMClassInfo::SetProperty Don't call me!");
  1.1401 +
  1.1402 +  return NS_ERROR_UNEXPECTED;
  1.1403 +}
  1.1404 +
  1.1405 +NS_IMETHODIMP
  1.1406 +nsDOMClassInfo::Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1407 +                          JSObject *obj, bool *_retval)
  1.1408 +{
  1.1409 +  return NS_OK;
  1.1410 +}
  1.1411 +
  1.1412 +NS_IMETHODIMP
  1.1413 +nsDOMClassInfo::NewEnumerate(nsIXPConnectWrappedNative *wrapper,
  1.1414 +                             JSContext *cx, JSObject *obj, uint32_t enum_op,
  1.1415 +                             jsval *statep, jsid *idp, bool *_retval)
  1.1416 +{
  1.1417 +  NS_WARNING("nsDOMClassInfo::NewEnumerate Don't call me!");
  1.1418 +
  1.1419 +  return NS_ERROR_UNEXPECTED;
  1.1420 +}
  1.1421 +
  1.1422 +nsresult
  1.1423 +nsDOMClassInfo::ResolveConstructor(JSContext *cx, JSObject *aObj,
  1.1424 +                                   JSObject **objp)
  1.1425 +{
  1.1426 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.1427 +  JS::Rooted<JSObject*> global(cx, ::JS_GetGlobalForObject(cx, obj));
  1.1428 +
  1.1429 +  JS::Rooted<JS::Value> val(cx);
  1.1430 +  if (!::JS_LookupProperty(cx, global, mData->mName, &val)) {
  1.1431 +    return NS_ERROR_UNEXPECTED;
  1.1432 +  }
  1.1433 +
  1.1434 +  if (!JSVAL_IS_PRIMITIVE(val)) {
  1.1435 +    // If val is not an (non-null) object there either is no
  1.1436 +    // constructor for this class, or someone messed with
  1.1437 +    // window.classname, just fall through and let the JS engine
  1.1438 +    // return the Object constructor.
  1.1439 +
  1.1440 +    if (!::JS_DefinePropertyById(cx, obj, sConstructor_id, val, JS_PropertyStub,
  1.1441 +                                 JS_StrictPropertyStub, JSPROP_ENUMERATE)) {
  1.1442 +      return NS_ERROR_UNEXPECTED;
  1.1443 +    }
  1.1444 +
  1.1445 +    *objp = obj;
  1.1446 +  }
  1.1447 +
  1.1448 +  return NS_OK;
  1.1449 +}
  1.1450 +
  1.1451 +NS_IMETHODIMP
  1.1452 +nsDOMClassInfo::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1453 +                           JSObject *obj, jsid id, JSObject **objp,
  1.1454 +                           bool *_retval)
  1.1455 +{
  1.1456 +  if (id == sConstructor_id) {
  1.1457 +    return ResolveConstructor(cx, obj, objp);
  1.1458 +  }
  1.1459 +
  1.1460 +  return NS_OK;
  1.1461 +}
  1.1462 +
  1.1463 +NS_IMETHODIMP
  1.1464 +nsDOMClassInfo::Convert(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1465 +                        JSObject *obj, uint32_t type, jsval *vp,
  1.1466 +                        bool *_retval)
  1.1467 +{
  1.1468 +  NS_WARNING("nsDOMClassInfo::Convert Don't call me!");
  1.1469 +
  1.1470 +  return NS_ERROR_UNEXPECTED;
  1.1471 +}
  1.1472 +
  1.1473 +NS_IMETHODIMP
  1.1474 +nsDOMClassInfo::Finalize(nsIXPConnectWrappedNative *wrapper, JSFreeOp *fop,
  1.1475 +                         JSObject *obj)
  1.1476 +{
  1.1477 +  NS_WARNING("nsDOMClassInfo::Finalize Don't call me!");
  1.1478 +
  1.1479 +  return NS_ERROR_UNEXPECTED;
  1.1480 +}
  1.1481 +
  1.1482 +NS_IMETHODIMP
  1.1483 +nsDOMClassInfo::Call(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1484 +                     JSObject *obj, const JS::CallArgs &args, bool *_retval)
  1.1485 +{
  1.1486 +  NS_WARNING("nsDOMClassInfo::Call Don't call me!");
  1.1487 +
  1.1488 +  return NS_ERROR_UNEXPECTED;
  1.1489 +}
  1.1490 +
  1.1491 +NS_IMETHODIMP
  1.1492 +nsDOMClassInfo::Construct(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1493 +                          JSObject *obj, const JS::CallArgs &args,
  1.1494 +                          bool *_retval)
  1.1495 +{
  1.1496 +  NS_WARNING("nsDOMClassInfo::Construct Don't call me!");
  1.1497 +
  1.1498 +  return NS_ERROR_UNEXPECTED;
  1.1499 +}
  1.1500 +
  1.1501 +NS_IMETHODIMP
  1.1502 +nsDOMClassInfo::HasInstance(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1503 +                            JSObject *obj, JS::Handle<JS::Value> val, bool *bp,
  1.1504 +                            bool *_retval)
  1.1505 +{
  1.1506 +  NS_WARNING("nsDOMClassInfo::HasInstance Don't call me!");
  1.1507 +
  1.1508 +  return NS_ERROR_UNEXPECTED;
  1.1509 +}
  1.1510 +
  1.1511 +NS_IMETHODIMP
  1.1512 +nsDOMClassInfo::OuterObject(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
  1.1513 +                            JSObject * obj, JSObject * *_retval)
  1.1514 +{
  1.1515 +  NS_WARNING("nsDOMClassInfo::OuterObject Don't call me!");
  1.1516 +
  1.1517 +  return NS_ERROR_UNEXPECTED;
  1.1518 +}
  1.1519 +
  1.1520 +static nsresult
  1.1521 +GetExternalClassInfo(nsScriptNameSpaceManager *aNameSpaceManager,
  1.1522 +                     const nsAString &aName,
  1.1523 +                     const nsGlobalNameStruct *aStruct,
  1.1524 +                     const nsGlobalNameStruct **aResult)
  1.1525 +{
  1.1526 +  NS_ASSERTION(aStruct->mType ==
  1.1527 +                 nsGlobalNameStruct::eTypeExternalClassInfoCreator,
  1.1528 +               "Wrong type!");
  1.1529 +
  1.1530 +  nsresult rv;
  1.1531 +  nsCOMPtr<nsIDOMCIExtension> creator(do_CreateInstance(aStruct->mCID, &rv));
  1.1532 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1533 +
  1.1534 +  nsCOMPtr<nsIDOMScriptObjectFactory> sof(do_GetService(kDOMSOF_CID));
  1.1535 +  NS_ENSURE_TRUE(sof, NS_ERROR_FAILURE);
  1.1536 +
  1.1537 +  rv = creator->RegisterDOMCI(NS_ConvertUTF16toUTF8(aName).get(), sof);
  1.1538 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1539 +
  1.1540 +  const nsGlobalNameStruct *name_struct = aNameSpaceManager->LookupName(aName);
  1.1541 +  if (name_struct &&
  1.1542 +      name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.1543 +    *aResult = name_struct;
  1.1544 +  }
  1.1545 +  else {
  1.1546 +    NS_ERROR("Couldn't get the DOM ClassInfo data.");
  1.1547 +
  1.1548 +    *aResult = nullptr;
  1.1549 +  }
  1.1550 +
  1.1551 +  return NS_OK;
  1.1552 +}
  1.1553 +
  1.1554 +
  1.1555 +static nsresult
  1.1556 +ResolvePrototype(nsIXPConnect *aXPConnect, nsGlobalWindow *aWin, JSContext *cx,
  1.1557 +                 JS::Handle<JSObject*> obj, const char16_t *name,
  1.1558 +                 const nsDOMClassInfoData *ci_data,
  1.1559 +                 const nsGlobalNameStruct *name_struct,
  1.1560 +                 nsScriptNameSpaceManager *nameSpaceManager,
  1.1561 +                 JSObject *dot_prototype,
  1.1562 +                 JS::MutableHandle<JSPropertyDescriptor> ctorDesc);
  1.1563 +
  1.1564 +NS_IMETHODIMP
  1.1565 +nsDOMClassInfo::PostCreatePrototype(JSContext * cx, JSObject * aProto)
  1.1566 +{
  1.1567 +  uint32_t flags = (mData->mScriptableFlags & DONT_ENUM_STATIC_PROPS)
  1.1568 +                   ? 0
  1.1569 +                   : JSPROP_ENUMERATE;
  1.1570 +
  1.1571 +  uint32_t count = 0;
  1.1572 +  while (mData->mInterfaces[count]) {
  1.1573 +    count++;
  1.1574 +  }
  1.1575 +
  1.1576 +  JS::Rooted<JSObject*> proto(cx, aProto);
  1.1577 +  if (!xpc::DOM_DefineQuickStubs(cx, proto, flags, count, mData->mInterfaces)) {
  1.1578 +    JS_ClearPendingException(cx);
  1.1579 +  }
  1.1580 +
  1.1581 +  // This is called before any other location that requires
  1.1582 +  // sObjectClass, so compute it here. We assume that nobody has had a
  1.1583 +  // chance to monkey around with proto's prototype chain before this.
  1.1584 +  if (!sObjectClass) {
  1.1585 +    FindObjectClass(cx, proto);
  1.1586 +    NS_ASSERTION(sObjectClass && !strcmp(sObjectClass->name, "Object"),
  1.1587 +                 "Incorrect object class!");
  1.1588 +  }
  1.1589 +
  1.1590 +#ifdef DEBUG
  1.1591 +    JS::Rooted<JSObject*> proto2(cx);
  1.1592 +    JS_GetPrototype(cx, proto, &proto2);
  1.1593 +    NS_ASSERTION(proto2 && JS_GetClass(proto2) == sObjectClass,
  1.1594 +                 "Hmm, somebody did something evil?");
  1.1595 +#endif
  1.1596 +
  1.1597 +#ifdef DEBUG
  1.1598 +  if (mData->mHasClassInterface && mData->mProtoChainInterface &&
  1.1599 +      mData->mProtoChainInterface != &NS_GET_IID(nsISupports)) {
  1.1600 +    nsCOMPtr<nsIInterfaceInfoManager>
  1.1601 +      iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
  1.1602 +
  1.1603 +    if (iim) {
  1.1604 +      nsCOMPtr<nsIInterfaceInfo> if_info;
  1.1605 +      iim->GetInfoForIID(mData->mProtoChainInterface,
  1.1606 +                         getter_AddRefs(if_info));
  1.1607 +
  1.1608 +      if (if_info) {
  1.1609 +        nsXPIDLCString name;
  1.1610 +        if_info->GetName(getter_Copies(name));
  1.1611 +        NS_ASSERTION(nsCRT::strcmp(CutPrefix(name), mData->mName) == 0,
  1.1612 +                     "Class name and proto chain interface name mismatch!");
  1.1613 +      }
  1.1614 +    }
  1.1615 +  }
  1.1616 +#endif
  1.1617 +
  1.1618 +  // Make prototype delegation work correctly. Consider if a site sets
  1.1619 +  // HTMLElement.prototype.foopy = function () { ... } Now, calling
  1.1620 +  // document.body.foopy() needs to ensure that looking up foopy on
  1.1621 +  // document.body's prototype will find the right function.
  1.1622 +  JS::Rooted<JSObject*> global(cx, ::JS_GetGlobalForObject(cx, proto));
  1.1623 +
  1.1624 +  // Only do this if the global object is a window.
  1.1625 +  // XXX Is there a better way to check this?
  1.1626 +  nsISupports *globalNative = XPConnect()->GetNativeOfWrapper(cx, global);
  1.1627 +  nsCOMPtr<nsPIDOMWindow> piwin = do_QueryInterface(globalNative);
  1.1628 +  if (!piwin) {
  1.1629 +    return NS_OK;
  1.1630 +  }
  1.1631 +
  1.1632 +  nsGlobalWindow *win = nsGlobalWindow::FromSupports(globalNative);
  1.1633 +  if (win->IsClosedOrClosing()) {
  1.1634 +    return NS_OK;
  1.1635 +  }
  1.1636 +
  1.1637 +  // If the window is in a different compartment than the global object, then
  1.1638 +  // it's likely that global is a sandbox object whose prototype is a window.
  1.1639 +  // Don't do anything in this case.
  1.1640 +  if (win->FastGetGlobalJSObject() &&
  1.1641 +      js::GetObjectCompartment(global) != js::GetObjectCompartment(win->FastGetGlobalJSObject())) {
  1.1642 +    return NS_OK;
  1.1643 +  }
  1.1644 +
  1.1645 +  if (win->IsOuterWindow()) {
  1.1646 +    // XXXjst: Do security checks here when we remove the security
  1.1647 +    // checks on the inner window.
  1.1648 +
  1.1649 +    win = win->GetCurrentInnerWindowInternal();
  1.1650 +
  1.1651 +    if (!win || !(global = win->GetGlobalJSObject()) ||
  1.1652 +        win->IsClosedOrClosing()) {
  1.1653 +      return NS_OK;
  1.1654 +    }
  1.1655 +  }
  1.1656 +
  1.1657 +  // Don't overwrite a property set by content.
  1.1658 +  bool contentDefinedProperty;
  1.1659 +  if (!::JS_AlreadyHasOwnUCProperty(cx, global, reinterpret_cast<const jschar*>(mData->mNameUTF16),
  1.1660 +                                    NS_strlen(mData->mNameUTF16),
  1.1661 +                                    &contentDefinedProperty)) {
  1.1662 +    return NS_ERROR_FAILURE;
  1.1663 +  }
  1.1664 +
  1.1665 +  nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
  1.1666 +  NS_ENSURE_TRUE(nameSpaceManager, NS_OK);
  1.1667 +
  1.1668 +  JS::Rooted<JSPropertyDescriptor> desc(cx);
  1.1669 +  nsresult rv = ResolvePrototype(sXPConnect, win, cx, global, mData->mNameUTF16,
  1.1670 +                                 mData, nullptr, nameSpaceManager, proto,
  1.1671 +                                 &desc);
  1.1672 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1673 +  if (!contentDefinedProperty && desc.object() && !desc.value().isUndefined() &&
  1.1674 +      !JS_DefineUCProperty(cx, global, mData->mNameUTF16,
  1.1675 +                           NS_strlen(mData->mNameUTF16),
  1.1676 +                           desc.value(), desc.getter(), desc.setter(),
  1.1677 +                           desc.attributes())) {
  1.1678 +    return NS_ERROR_UNEXPECTED;
  1.1679 +  }
  1.1680 +
  1.1681 +  return NS_OK;
  1.1682 +}
  1.1683 +
  1.1684 +// static
  1.1685 +nsIClassInfo *
  1.1686 +NS_GetDOMClassInfoInstance(nsDOMClassInfoID aID)
  1.1687 +{
  1.1688 +  if (aID >= eDOMClassInfoIDCount) {
  1.1689 +    NS_ERROR("Bad ID!");
  1.1690 +
  1.1691 +    return nullptr;
  1.1692 +  }
  1.1693 +
  1.1694 +  if (!nsDOMClassInfo::sIsInitialized) {
  1.1695 +    nsresult rv = nsDOMClassInfo::Init();
  1.1696 +
  1.1697 +    NS_ENSURE_SUCCESS(rv, nullptr);
  1.1698 +  }
  1.1699 +
  1.1700 +  if (!sClassInfoData[aID].mCachedClassInfo) {
  1.1701 +    nsDOMClassInfoData& data = sClassInfoData[aID];
  1.1702 +
  1.1703 +    data.mCachedClassInfo = data.u.mConstructorFptr(&data);
  1.1704 +    NS_ENSURE_TRUE(data.mCachedClassInfo, nullptr);
  1.1705 +
  1.1706 +    NS_ADDREF(data.mCachedClassInfo);
  1.1707 +  }
  1.1708 +
  1.1709 +  NS_ASSERTION(!IS_EXTERNAL(sClassInfoData[aID].mCachedClassInfo),
  1.1710 +               "This is bad, internal class marked as external!");
  1.1711 +
  1.1712 +  return sClassInfoData[aID].mCachedClassInfo;
  1.1713 +}
  1.1714 +
  1.1715 +// static
  1.1716 +nsIClassInfo *
  1.1717 +nsDOMClassInfo::GetClassInfoInstance(nsDOMClassInfoData* aData)
  1.1718 +{
  1.1719 +  NS_ASSERTION(IS_EXTERNAL(aData->mCachedClassInfo)
  1.1720 +               || !aData->mCachedClassInfo,
  1.1721 +               "This is bad, external class marked as internal!");
  1.1722 +
  1.1723 +  if (!aData->mCachedClassInfo) {
  1.1724 +    if (aData->u.mExternalConstructorFptr) {
  1.1725 +      aData->mCachedClassInfo =
  1.1726 +        aData->u.mExternalConstructorFptr(aData->mName);
  1.1727 +    } else {
  1.1728 +      aData->mCachedClassInfo = nsDOMGenericSH::doCreate(aData);
  1.1729 +    }
  1.1730 +    NS_ENSURE_TRUE(aData->mCachedClassInfo, nullptr);
  1.1731 +
  1.1732 +    NS_ADDREF(aData->mCachedClassInfo);
  1.1733 +    aData->mCachedClassInfo = MARK_EXTERNAL(aData->mCachedClassInfo);
  1.1734 +  }
  1.1735 +
  1.1736 +  return GET_CLEAN_CI_PTR(aData->mCachedClassInfo);
  1.1737 +}
  1.1738 +
  1.1739 +
  1.1740 +// static
  1.1741 +void
  1.1742 +nsDOMClassInfo::ShutDown()
  1.1743 +{
  1.1744 +  if (sClassInfoData[0].u.mConstructorFptr) {
  1.1745 +    uint32_t i;
  1.1746 +
  1.1747 +    for (i = 0; i < eDOMClassInfoIDCount; i++) {
  1.1748 +      NS_IF_RELEASE(sClassInfoData[i].mCachedClassInfo);
  1.1749 +    }
  1.1750 +  }
  1.1751 +
  1.1752 +  sLocation_id        = JSID_VOID;
  1.1753 +  sConstructor_id     = JSID_VOID;
  1.1754 +  sLength_id          = JSID_VOID;
  1.1755 +  sItem_id            = JSID_VOID;
  1.1756 +  sEnumerate_id       = JSID_VOID;
  1.1757 +  sTop_id             = JSID_VOID;
  1.1758 +  sDocument_id        = JSID_VOID;
  1.1759 +  sWrappedJSObject_id = JSID_VOID;
  1.1760 +
  1.1761 +  NS_IF_RELEASE(sXPConnect);
  1.1762 +  NS_IF_RELEASE(sSecMan);
  1.1763 +  sIsInitialized = false;
  1.1764 +}
  1.1765 +
  1.1766 +// Window helper
  1.1767 +
  1.1768 +NS_IMETHODIMP
  1.1769 +nsWindowSH::PreCreate(nsISupports *nativeObj, JSContext *cx,
  1.1770 +                      JSObject *globalObj, JSObject **parentObj)
  1.1771 +{
  1.1772 +  // Normally ::PreCreate() is used to give XPConnect the parent
  1.1773 +  // object for the object that's being wrapped, this parent object is
  1.1774 +  // set as the parent of the wrapper and it's also used to find the
  1.1775 +  // right scope for the object being wrapped. Now, in the case of the
  1.1776 +  // global object the wrapper shouldn't have a parent but we supply
  1.1777 +  // one here anyway (the global object itself) and this will be used
  1.1778 +  // by XPConnect only to find the right scope, once the scope is
  1.1779 +  // found XPConnect will find the existing wrapper (which always
  1.1780 +  // exists since it's created on window construction), since an
  1.1781 +  // existing wrapper is found the parent we supply here is ignored
  1.1782 +  // after the wrapper is found.
  1.1783 +
  1.1784 +  nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(nativeObj));
  1.1785 +  NS_ASSERTION(sgo, "nativeObj not a global object!");
  1.1786 +
  1.1787 +  nsGlobalWindow *win = nsGlobalWindow::FromSupports(nativeObj);
  1.1788 +  NS_ASSERTION(win->IsInnerWindow(), "Should be inner window.");
  1.1789 +
  1.1790 +  // We sometimes get a disconnected window during file api test. :-(
  1.1791 +  if (!win->GetOuterWindowInternal())
  1.1792 +    return NS_ERROR_FAILURE;
  1.1793 +
  1.1794 +  // If we're bootstrapping, we don't have a JS object yet.
  1.1795 +  if (win->GetOuterWindowInternal()->IsCreatingInnerWindow())
  1.1796 +    return NS_OK;
  1.1797 +
  1.1798 +  return SetParentToWindow(win, parentObj);
  1.1799 +}
  1.1800 +
  1.1801 +NS_IMETHODIMP
  1.1802 +nsWindowSH::PostCreatePrototype(JSContext* aCx, JSObject* aProto)
  1.1803 +{
  1.1804 +  JS::Rooted<JSObject*> proto(aCx, aProto);
  1.1805 +
  1.1806 +  nsresult rv = nsDOMClassInfo::PostCreatePrototype(aCx, proto);
  1.1807 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1808 +
  1.1809 +  // We should probably move this into the CreateInterfaceObjects for Window
  1.1810 +  // once it is on WebIDL bindings.
  1.1811 +  WindowNamedPropertiesHandler::Install(aCx, proto);
  1.1812 +  return NS_OK;
  1.1813 +}
  1.1814 +
  1.1815 +NS_IMETHODIMP
  1.1816 +nsWindowSH::PostCreate(nsIXPConnectWrappedNative *wrapper,
  1.1817 +                       JSContext *cx, JSObject *obj)
  1.1818 +{
  1.1819 +  JS::Rooted<JSObject*> window(cx, obj);
  1.1820 +
  1.1821 +#ifdef DEBUG
  1.1822 +  nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryWrappedNative(wrapper));
  1.1823 +
  1.1824 +  NS_ASSERTION(sgo && sgo->GetGlobalJSObject() == obj,
  1.1825 +               "Multiple wrappers created for global object!");
  1.1826 +#endif
  1.1827 +
  1.1828 +  const NativeProperties* windowProperties =
  1.1829 +    WindowBinding::sNativePropertyHooks->mNativeProperties.regular;
  1.1830 +  const NativeProperties* eventTargetProperties =
  1.1831 +    EventTargetBinding::sNativePropertyHooks->mNativeProperties.regular;
  1.1832 +
  1.1833 +  return DefineWebIDLBindingPropertiesOnXPCObject(cx, window, windowProperties, true) &&
  1.1834 +         DefineWebIDLBindingPropertiesOnXPCObject(cx, window, eventTargetProperties, true) ?
  1.1835 +         NS_OK : NS_ERROR_FAILURE;
  1.1836 +}
  1.1837 +
  1.1838 +struct ResolveGlobalNameClosure
  1.1839 +{
  1.1840 +  JSContext* cx;
  1.1841 +  JS::Handle<JSObject*> obj;
  1.1842 +  bool* retval;
  1.1843 +};
  1.1844 +
  1.1845 +static PLDHashOperator
  1.1846 +ResolveGlobalName(const nsAString& aName,
  1.1847 +                  const nsGlobalNameStruct& aNameStruct,
  1.1848 +                  void* aClosure)
  1.1849 +{
  1.1850 +  ResolveGlobalNameClosure* closure =
  1.1851 +    static_cast<ResolveGlobalNameClosure*>(aClosure);
  1.1852 +  JS::Rooted<JS::Value> dummy(closure->cx);
  1.1853 +  bool ok = JS_LookupUCProperty(closure->cx, closure->obj,
  1.1854 +                                aName.BeginReading(), aName.Length(),
  1.1855 +                                &dummy);
  1.1856 +  if (!ok) {
  1.1857 +    *closure->retval = false;
  1.1858 +    return PL_DHASH_STOP;
  1.1859 +  }
  1.1860 +  return PL_DHASH_NEXT;
  1.1861 +}
  1.1862 +
  1.1863 +NS_IMETHODIMP
  1.1864 +nsWindowSH::Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.1865 +                      JSObject *aObj, bool *_retval)
  1.1866 +{
  1.1867 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.1868 +  if (!xpc::WrapperFactory::IsXrayWrapper(obj)) {
  1.1869 +    *_retval = JS_EnumerateStandardClasses(cx, obj);
  1.1870 +    if (!*_retval) {
  1.1871 +      return NS_OK;
  1.1872 +    }
  1.1873 +
  1.1874 +    // Now resolve everything from the namespace manager
  1.1875 +    nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
  1.1876 +    if (!nameSpaceManager) {
  1.1877 +      NS_ERROR("Can't get namespace manager.");
  1.1878 +      return NS_ERROR_UNEXPECTED;
  1.1879 +    }
  1.1880 +    ResolveGlobalNameClosure closure = { cx, obj, _retval };
  1.1881 +    nameSpaceManager->EnumerateGlobalNames(ResolveGlobalName, &closure);
  1.1882 +  }
  1.1883 +
  1.1884 +  return NS_OK;
  1.1885 +}
  1.1886 +
  1.1887 +static nsDOMConstructorFunc
  1.1888 +FindConstructorFunc(const nsDOMClassInfoData *aDOMClassInfoData)
  1.1889 +{
  1.1890 +  for (uint32_t i = 0; i < ArrayLength(kConstructorFuncMap); ++i) {
  1.1891 +    if (&sClassInfoData[kConstructorFuncMap[i].mDOMClassInfoID] ==
  1.1892 +        aDOMClassInfoData) {
  1.1893 +      return kConstructorFuncMap[i].mConstructorFunc;
  1.1894 +    }
  1.1895 +  }
  1.1896 +  return nullptr;
  1.1897 +}
  1.1898 +
  1.1899 +static nsresult
  1.1900 +BaseStubConstructor(nsIWeakReference* aWeakOwner,
  1.1901 +                    const nsGlobalNameStruct *name_struct, JSContext *cx,
  1.1902 +                    JS::Handle<JSObject*> obj, const JS::CallArgs &args)
  1.1903 +{
  1.1904 +  MOZ_ASSERT(obj);
  1.1905 +
  1.1906 +  nsresult rv;
  1.1907 +  nsCOMPtr<nsISupports> native;
  1.1908 +  if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.1909 +    const nsDOMClassInfoData* ci_data =
  1.1910 +      &sClassInfoData[name_struct->mDOMClassInfoID];
  1.1911 +    nsDOMConstructorFunc func = FindConstructorFunc(ci_data);
  1.1912 +    if (func) {
  1.1913 +      rv = func(getter_AddRefs(native));
  1.1914 +    } else {
  1.1915 +      rv = NS_ERROR_NOT_AVAILABLE;
  1.1916 +    }
  1.1917 +  } else if (name_struct->mType == nsGlobalNameStruct::eTypeExternalConstructor) {
  1.1918 +    native = do_CreateInstance(name_struct->mCID, &rv);
  1.1919 +  } else if (name_struct->mType == nsGlobalNameStruct::eTypeExternalConstructorAlias) {
  1.1920 +    native = do_CreateInstance(name_struct->mAlias->mCID, &rv);
  1.1921 +  } else {
  1.1922 +    native = do_CreateInstance(*name_struct->mData->mConstructorCID, &rv);
  1.1923 +  }
  1.1924 +  if (NS_FAILED(rv)) {
  1.1925 +    NS_ERROR("Failed to create the object");
  1.1926 +    return rv;
  1.1927 +  }
  1.1928 +
  1.1929 +  nsCOMPtr<nsIJSNativeInitializer> initializer(do_QueryInterface(native));
  1.1930 +  nsCOMPtr<nsIDOMGlobalObjectConstructor> constructor(do_QueryInterface(native));
  1.1931 +  if (initializer || constructor) {
  1.1932 +    // Initialize object using the current inner window, but only if
  1.1933 +    // the caller can access it.
  1.1934 +    nsCOMPtr<nsPIDOMWindow> owner = do_QueryReferent(aWeakOwner);
  1.1935 +    nsPIDOMWindow* outerWindow = owner ? owner->GetOuterWindow() : nullptr;
  1.1936 +    nsPIDOMWindow* currentInner =
  1.1937 +      outerWindow ? outerWindow->GetCurrentInnerWindow() : nullptr;
  1.1938 +    if (!currentInner ||
  1.1939 +        (owner != currentInner &&
  1.1940 +         !nsContentUtils::CanCallerAccess(currentInner))) {
  1.1941 +      return NS_ERROR_DOM_SECURITY_ERR;
  1.1942 +    }
  1.1943 +
  1.1944 +    if (initializer) {
  1.1945 +      rv = initializer->Initialize(currentInner, cx, obj, args);
  1.1946 +      if (NS_FAILED(rv)) {
  1.1947 +        return rv;
  1.1948 +      }
  1.1949 +    } else {
  1.1950 +      nsCOMPtr<nsIXPConnectWrappedJS> wrappedJS = do_QueryInterface(native);
  1.1951 +
  1.1952 +      JS::Rooted<JSObject*> thisObject(cx, wrappedJS->GetJSObject());
  1.1953 +      if (!thisObject) {
  1.1954 +        return NS_ERROR_UNEXPECTED;
  1.1955 +      }
  1.1956 +
  1.1957 +      nsCxPusher pusher;
  1.1958 +      pusher.Push(cx);
  1.1959 +
  1.1960 +      JSAutoCompartment ac(cx, thisObject);
  1.1961 +
  1.1962 +      JS::Rooted<JS::Value> funval(cx);
  1.1963 +      if (!JS_GetProperty(cx, thisObject, "constructor", &funval) ||
  1.1964 +          !funval.isObject()) {
  1.1965 +        return NS_ERROR_UNEXPECTED;
  1.1966 +      }
  1.1967 +
  1.1968 +      // Check if the object is even callable.
  1.1969 +      NS_ENSURE_STATE(JS_ObjectIsCallable(cx, &funval.toObject()));
  1.1970 +      {
  1.1971 +        // wrap parameters in the target compartment
  1.1972 +        // we also pass in the calling window as the first argument
  1.1973 +        unsigned argc = args.length() + 1;
  1.1974 +        JS::AutoValueVector argv(cx);
  1.1975 +        if (!argv.resize(argc)) {
  1.1976 +          return NS_ERROR_OUT_OF_MEMORY;
  1.1977 +        }
  1.1978 +
  1.1979 +        nsCOMPtr<nsIDOMWindow> currentWin(do_GetInterface(currentInner));
  1.1980 +        rv = WrapNative(cx, currentWin, &NS_GET_IID(nsIDOMWindow),
  1.1981 +                        true, argv.handleAt(0));
  1.1982 +
  1.1983 +        for (size_t i = 1; i < argc; ++i) {
  1.1984 +          argv[i] = args[i - 1];
  1.1985 +          if (!JS_WrapValue(cx, argv.handleAt(i)))
  1.1986 +            return NS_ERROR_FAILURE;
  1.1987 +        }
  1.1988 +
  1.1989 +        JS::Rooted<JS::Value> frval(cx);
  1.1990 +        bool ret = JS_CallFunctionValue(cx, thisObject, funval, argv, &frval);
  1.1991 +
  1.1992 +        if (!ret) {
  1.1993 +          return NS_ERROR_FAILURE;
  1.1994 +        }
  1.1995 +      }
  1.1996 +    }
  1.1997 +  }
  1.1998 +
  1.1999 +  js::AssertSameCompartment(cx, obj);
  1.2000 +  return WrapNative(cx, native, true, args.rval());
  1.2001 +}
  1.2002 +
  1.2003 +static nsresult
  1.2004 +DefineInterfaceConstants(JSContext *cx, JS::Handle<JSObject*> obj, const nsIID *aIID)
  1.2005 +{
  1.2006 +  nsCOMPtr<nsIInterfaceInfoManager>
  1.2007 +    iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
  1.2008 +  NS_ENSURE_TRUE(iim, NS_ERROR_UNEXPECTED);
  1.2009 +
  1.2010 +  nsCOMPtr<nsIInterfaceInfo> if_info;
  1.2011 +
  1.2012 +  nsresult rv = iim->GetInfoForIID(aIID, getter_AddRefs(if_info));
  1.2013 +  NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && if_info, rv);
  1.2014 +
  1.2015 +  uint16_t constant_count;
  1.2016 +
  1.2017 +  if_info->GetConstantCount(&constant_count);
  1.2018 +
  1.2019 +  if (!constant_count) {
  1.2020 +    return NS_OK;
  1.2021 +  }
  1.2022 +
  1.2023 +  nsCOMPtr<nsIInterfaceInfo> parent_if_info;
  1.2024 +
  1.2025 +  rv = if_info->GetParent(getter_AddRefs(parent_if_info));
  1.2026 +  NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && parent_if_info, rv);
  1.2027 +
  1.2028 +  uint16_t parent_constant_count, i;
  1.2029 +  parent_if_info->GetConstantCount(&parent_constant_count);
  1.2030 +
  1.2031 +  JS::Rooted<JS::Value> v(cx);
  1.2032 +  for (i = parent_constant_count; i < constant_count; i++) {
  1.2033 +    const nsXPTConstant *c = nullptr;
  1.2034 +
  1.2035 +    rv = if_info->GetConstant(i, &c);
  1.2036 +    NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && c, rv);
  1.2037 +
  1.2038 +    uint16_t type = c->GetType().TagPart();
  1.2039 +
  1.2040 +    v.setUndefined();
  1.2041 +    switch (type) {
  1.2042 +      case nsXPTType::T_I8:
  1.2043 +      case nsXPTType::T_U8:
  1.2044 +      {
  1.2045 +        v.setInt32(c->GetValue()->val.u8);
  1.2046 +        break;
  1.2047 +      }
  1.2048 +      case nsXPTType::T_I16:
  1.2049 +      case nsXPTType::T_U16:
  1.2050 +      {
  1.2051 +        v.setInt32(c->GetValue()->val.u16);
  1.2052 +        break;
  1.2053 +      }
  1.2054 +      case nsXPTType::T_I32:
  1.2055 +      {
  1.2056 +        v = JS_NumberValue(c->GetValue()->val.i32);
  1.2057 +        break;
  1.2058 +      }
  1.2059 +      case nsXPTType::T_U32:
  1.2060 +      {
  1.2061 +        v = JS_NumberValue(c->GetValue()->val.u32);
  1.2062 +        break;
  1.2063 +      }
  1.2064 +      default:
  1.2065 +      {
  1.2066 +#ifdef DEBUG
  1.2067 +        NS_ERROR("Non-numeric constant found in interface.");
  1.2068 +#endif
  1.2069 +        continue;
  1.2070 +      }
  1.2071 +    }
  1.2072 +
  1.2073 +    if (!::JS_DefineProperty(cx, obj, c->GetName(), v,
  1.2074 +                             JSPROP_ENUMERATE | JSPROP_READONLY |
  1.2075 +                             JSPROP_PERMANENT,
  1.2076 +                             JS_PropertyStub, JS_StrictPropertyStub)) {
  1.2077 +      return NS_ERROR_UNEXPECTED;
  1.2078 +    }
  1.2079 +  }
  1.2080 +
  1.2081 +  return NS_OK;
  1.2082 +}
  1.2083 +
  1.2084 +class nsDOMConstructor MOZ_FINAL : public nsIDOMDOMConstructor
  1.2085 +{
  1.2086 +protected:
  1.2087 +  nsDOMConstructor(const char16_t* aName,
  1.2088 +                   bool aIsConstructable,
  1.2089 +                   nsPIDOMWindow* aOwner)
  1.2090 +    : mClassName(aName),
  1.2091 +      mConstructable(aIsConstructable),
  1.2092 +      mWeakOwner(do_GetWeakReference(aOwner))
  1.2093 +  {
  1.2094 +  }
  1.2095 +
  1.2096 +public:
  1.2097 +
  1.2098 +  static nsresult Create(const char16_t* aName,
  1.2099 +                         const nsDOMClassInfoData* aData,
  1.2100 +                         const nsGlobalNameStruct* aNameStruct,
  1.2101 +                         nsPIDOMWindow* aOwner,
  1.2102 +                         nsDOMConstructor** aResult);
  1.2103 +
  1.2104 +  NS_DECL_ISUPPORTS
  1.2105 +  NS_DECL_NSIDOMDOMCONSTRUCTOR
  1.2106 +
  1.2107 +  nsresult PreCreate(JSContext *cx, JSObject *globalObj, JSObject **parentObj);
  1.2108 +
  1.2109 +  nsresult Construct(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.2110 +                     JS::Handle<JSObject*> obj, const JS::CallArgs &args,
  1.2111 +                     bool *_retval);
  1.2112 +
  1.2113 +  nsresult HasInstance(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.2114 +                       JS::Handle<JSObject*> obj, const jsval &val, bool *bp,
  1.2115 +                       bool *_retval);
  1.2116 +
  1.2117 +  nsresult ResolveInterfaceConstants(JSContext *cx, JS::Handle<JSObject*> obj);
  1.2118 +
  1.2119 +private:
  1.2120 +  const nsGlobalNameStruct *GetNameStruct()
  1.2121 +  {
  1.2122 +    if (!mClassName) {
  1.2123 +      NS_ERROR("Can't get name");
  1.2124 +      return nullptr;
  1.2125 +    }
  1.2126 +
  1.2127 +    const nsGlobalNameStruct *nameStruct;
  1.2128 +#ifdef DEBUG
  1.2129 +    nsresult rv =
  1.2130 +#endif
  1.2131 +      GetNameStruct(nsDependentString(mClassName), &nameStruct);
  1.2132 +
  1.2133 +    NS_ASSERTION(NS_FAILED(rv) || nameStruct, "Name isn't in hash.");
  1.2134 +
  1.2135 +    return nameStruct;
  1.2136 +  }
  1.2137 +
  1.2138 +  static nsresult GetNameStruct(const nsAString& aName,
  1.2139 +                                const nsGlobalNameStruct **aNameStruct)
  1.2140 +  {
  1.2141 +    *aNameStruct = nullptr;
  1.2142 +
  1.2143 +    nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
  1.2144 +    if (!nameSpaceManager) {
  1.2145 +      NS_ERROR("Can't get namespace manager.");
  1.2146 +      return NS_ERROR_UNEXPECTED;
  1.2147 +    }
  1.2148 +
  1.2149 +    *aNameStruct = nameSpaceManager->LookupName(aName);
  1.2150 +
  1.2151 +    // Return NS_OK here, aName just isn't a DOM class but nothing failed.
  1.2152 +    return NS_OK;
  1.2153 +  }
  1.2154 +
  1.2155 +  static bool IsConstructable(const nsDOMClassInfoData *aData)
  1.2156 +  {
  1.2157 +    if (IS_EXTERNAL(aData->mCachedClassInfo)) {
  1.2158 +      const nsExternalDOMClassInfoData* data =
  1.2159 +        static_cast<const nsExternalDOMClassInfoData*>(aData);
  1.2160 +      return data->mConstructorCID != nullptr;
  1.2161 +    }
  1.2162 +
  1.2163 +    return FindConstructorFunc(aData);
  1.2164 +  }
  1.2165 +  static bool IsConstructable(const nsGlobalNameStruct *aNameStruct)
  1.2166 +  {
  1.2167 +    return
  1.2168 +      (aNameStruct->mType == nsGlobalNameStruct::eTypeClassConstructor &&
  1.2169 +       IsConstructable(&sClassInfoData[aNameStruct->mDOMClassInfoID])) ||
  1.2170 +      (aNameStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo &&
  1.2171 +       IsConstructable(aNameStruct->mData)) ||
  1.2172 +      aNameStruct->mType == nsGlobalNameStruct::eTypeExternalConstructor ||
  1.2173 +      aNameStruct->mType == nsGlobalNameStruct::eTypeExternalConstructorAlias;
  1.2174 +  }
  1.2175 +
  1.2176 +  const char16_t*   mClassName;
  1.2177 +  const bool mConstructable;
  1.2178 +  nsWeakPtr          mWeakOwner;
  1.2179 +};
  1.2180 +
  1.2181 +//static
  1.2182 +nsresult
  1.2183 +nsDOMConstructor::Create(const char16_t* aName,
  1.2184 +                         const nsDOMClassInfoData* aData,
  1.2185 +                         const nsGlobalNameStruct* aNameStruct,
  1.2186 +                         nsPIDOMWindow* aOwner,
  1.2187 +                         nsDOMConstructor** aResult)
  1.2188 +{
  1.2189 +  *aResult = nullptr;
  1.2190 +  // Prevent creating a constructor if aOwner is inner window which doesn't have
  1.2191 +  // an outer window. If the outer window doesn't have an inner window or the
  1.2192 +  // caller can't access the outer window's current inner window then try to use
  1.2193 +  // the owner (so long as it is, in fact, an inner window). If that doesn't
  1.2194 +  // work then prevent creation also.
  1.2195 +  nsPIDOMWindow* outerWindow = aOwner->GetOuterWindow();
  1.2196 +  nsPIDOMWindow* currentInner =
  1.2197 +    outerWindow ? outerWindow->GetCurrentInnerWindow() : aOwner;
  1.2198 +  if (!currentInner ||
  1.2199 +      (aOwner != currentInner &&
  1.2200 +       !nsContentUtils::CanCallerAccess(currentInner) &&
  1.2201 +       !(currentInner = aOwner)->IsInnerWindow())) {
  1.2202 +    return NS_ERROR_DOM_SECURITY_ERR;
  1.2203 +  }
  1.2204 +
  1.2205 +  bool constructable = aNameStruct ?
  1.2206 +                         IsConstructable(aNameStruct) :
  1.2207 +                         IsConstructable(aData);
  1.2208 +
  1.2209 +  *aResult = new nsDOMConstructor(aName, constructable, currentInner);
  1.2210 +  NS_ENSURE_TRUE(*aResult, NS_ERROR_OUT_OF_MEMORY);
  1.2211 +  NS_ADDREF(*aResult);
  1.2212 +  return NS_OK;
  1.2213 +}
  1.2214 +
  1.2215 +NS_IMPL_ADDREF(nsDOMConstructor)
  1.2216 +NS_IMPL_RELEASE(nsDOMConstructor)
  1.2217 +NS_INTERFACE_MAP_BEGIN(nsDOMConstructor)
  1.2218 +  NS_INTERFACE_MAP_ENTRY(nsIDOMDOMConstructor)
  1.2219 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
  1.2220 +  if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
  1.2221 +#ifdef DEBUG
  1.2222 +    {
  1.2223 +      const nsGlobalNameStruct *name_struct = GetNameStruct();
  1.2224 +      NS_ASSERTION(!name_struct ||
  1.2225 +                   mConstructable == IsConstructable(name_struct),
  1.2226 +                   "Can't change constructability dynamically!");
  1.2227 +    }
  1.2228 +#endif
  1.2229 +    foundInterface =
  1.2230 +      NS_GetDOMClassInfoInstance(mConstructable ?
  1.2231 +                                 eDOMClassInfo_DOMConstructor_id :
  1.2232 +                                 eDOMClassInfo_DOMPrototype_id);
  1.2233 +    if (!foundInterface) {
  1.2234 +      *aInstancePtr = nullptr;
  1.2235 +      return NS_ERROR_OUT_OF_MEMORY;
  1.2236 +    }
  1.2237 +  } else
  1.2238 +NS_INTERFACE_MAP_END
  1.2239 +
  1.2240 +nsresult
  1.2241 +nsDOMConstructor::PreCreate(JSContext *cx, JSObject *globalObj, JSObject **parentObj)
  1.2242 +{
  1.2243 +  nsCOMPtr<nsPIDOMWindow> owner(do_QueryReferent(mWeakOwner));
  1.2244 +  if (!owner) {
  1.2245 +    // Can't do anything.
  1.2246 +    return NS_OK;
  1.2247 +  }
  1.2248 +
  1.2249 +  nsGlobalWindow *win = static_cast<nsGlobalWindow *>(owner.get());
  1.2250 +  return SetParentToWindow(win, parentObj);
  1.2251 +}
  1.2252 +
  1.2253 +nsresult
  1.2254 +nsDOMConstructor::Construct(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
  1.2255 +                            JS::Handle<JSObject*> obj, const JS::CallArgs &args,
  1.2256 +                            bool *_retval)
  1.2257 +{
  1.2258 +  MOZ_ASSERT(obj);
  1.2259 +
  1.2260 +  const nsGlobalNameStruct *name_struct = GetNameStruct();
  1.2261 +  NS_ENSURE_TRUE(name_struct, NS_ERROR_FAILURE);
  1.2262 +
  1.2263 +  if (!IsConstructable(name_struct)) {
  1.2264 +    // ignore return value, we return false anyway
  1.2265 +    return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
  1.2266 +  }
  1.2267 +
  1.2268 +  return BaseStubConstructor(mWeakOwner, name_struct, cx, obj, args);
  1.2269 +}
  1.2270 +
  1.2271 +nsresult
  1.2272 +nsDOMConstructor::HasInstance(nsIXPConnectWrappedNative *wrapper,
  1.2273 +                              JSContext * cx, JS::Handle<JSObject*> obj,
  1.2274 +                              const jsval &v, bool *bp, bool *_retval)
  1.2275 +
  1.2276 +{
  1.2277 +  // No need to look these up in the hash.
  1.2278 +  *bp = false;
  1.2279 +  if (JSVAL_IS_PRIMITIVE(v)) {
  1.2280 +    return NS_OK;
  1.2281 +  }
  1.2282 +
  1.2283 +  JS::Rooted<JSObject*> dom_obj(cx, v.toObjectOrNull());
  1.2284 +  NS_ASSERTION(dom_obj, "nsDOMConstructor::HasInstance couldn't get object");
  1.2285 +
  1.2286 +  // This might not be the right object, if there are wrappers. Unwrap if we can.
  1.2287 +  JSObject *wrapped_obj = js::CheckedUnwrap(dom_obj, /* stopAtOuter = */ false);
  1.2288 +  if (wrapped_obj)
  1.2289 +      dom_obj = wrapped_obj;
  1.2290 +
  1.2291 +  const JSClass *dom_class = JS_GetClass(dom_obj);
  1.2292 +  if (!dom_class) {
  1.2293 +    NS_ERROR("nsDOMConstructor::HasInstance can't get class.");
  1.2294 +    return NS_ERROR_UNEXPECTED;
  1.2295 +  }
  1.2296 +
  1.2297 +  const nsGlobalNameStruct *name_struct;
  1.2298 +  nsresult rv = GetNameStruct(NS_ConvertASCIItoUTF16(dom_class->name), &name_struct);
  1.2299 +  if (NS_FAILED(rv)) {
  1.2300 +    return rv;
  1.2301 +  }
  1.2302 +
  1.2303 +  if (!name_struct) {
  1.2304 +    // This isn't a normal DOM object, see if this constructor lives on its
  1.2305 +    // prototype chain.
  1.2306 +    JS::Rooted<JS::Value> val(cx);
  1.2307 +    if (!JS_GetProperty(cx, obj, "prototype", &val)) {
  1.2308 +      return NS_ERROR_UNEXPECTED;
  1.2309 +    }
  1.2310 +
  1.2311 +    if (JSVAL_IS_PRIMITIVE(val)) {
  1.2312 +      return NS_OK;
  1.2313 +    }
  1.2314 +
  1.2315 +    JS::Rooted<JSObject*> dot_prototype(cx, val.toObjectOrNull());
  1.2316 +
  1.2317 +    JS::Rooted<JSObject*> proto(cx, dom_obj);
  1.2318 +    for (;;) {
  1.2319 +      if (!JS_GetPrototype(cx, proto, &proto)) {
  1.2320 +        return NS_ERROR_UNEXPECTED;
  1.2321 +      }
  1.2322 +      if (!proto) {
  1.2323 +        break;
  1.2324 +      }
  1.2325 +      if (proto == dot_prototype) {
  1.2326 +        *bp = true;
  1.2327 +        break;
  1.2328 +      }
  1.2329 +    }
  1.2330 +
  1.2331 +    return NS_OK;
  1.2332 +  }
  1.2333 +
  1.2334 +  if (name_struct->mType != nsGlobalNameStruct::eTypeClassConstructor &&
  1.2335 +      name_struct->mType != nsGlobalNameStruct::eTypeExternalClassInfo &&
  1.2336 +      name_struct->mType != nsGlobalNameStruct::eTypeExternalConstructorAlias) {
  1.2337 +    // Doesn't have DOM interfaces.
  1.2338 +    return NS_OK;
  1.2339 +  }
  1.2340 +
  1.2341 +  const nsGlobalNameStruct *class_name_struct = GetNameStruct();
  1.2342 +  NS_ENSURE_TRUE(class_name_struct, NS_ERROR_FAILURE);
  1.2343 +
  1.2344 +  if (name_struct == class_name_struct) {
  1.2345 +    *bp = true;
  1.2346 +
  1.2347 +    return NS_OK;
  1.2348 +  }
  1.2349 +
  1.2350 +  nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
  1.2351 +  NS_ASSERTION(nameSpaceManager, "Can't get namespace manager?");
  1.2352 +
  1.2353 +  const nsIID *class_iid;
  1.2354 +  if (class_name_struct->mType == nsGlobalNameStruct::eTypeInterface ||
  1.2355 +      class_name_struct->mType == nsGlobalNameStruct::eTypeClassProto) {
  1.2356 +    class_iid = &class_name_struct->mIID;
  1.2357 +  } else if (class_name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.2358 +    class_iid =
  1.2359 +      sClassInfoData[class_name_struct->mDOMClassInfoID].mProtoChainInterface;
  1.2360 +  } else if (class_name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.2361 +    class_iid = class_name_struct->mData->mProtoChainInterface;
  1.2362 +  } else if (class_name_struct->mType == nsGlobalNameStruct::eTypeExternalConstructorAlias) {
  1.2363 +    const nsGlobalNameStruct* alias_struct =
  1.2364 +      nameSpaceManager->GetConstructorProto(class_name_struct);
  1.2365 +    if (!alias_struct) {
  1.2366 +      NS_ERROR("Couldn't get constructor prototype.");
  1.2367 +      return NS_ERROR_UNEXPECTED;
  1.2368 +    }
  1.2369 +
  1.2370 +    if (alias_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.2371 +      class_iid =
  1.2372 +        sClassInfoData[alias_struct->mDOMClassInfoID].mProtoChainInterface;
  1.2373 +    } else if (alias_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.2374 +      class_iid = alias_struct->mData->mProtoChainInterface;
  1.2375 +    } else {
  1.2376 +      NS_ERROR("Expected eTypeClassConstructor or eTypeExternalClassInfo.");
  1.2377 +      return NS_ERROR_UNEXPECTED;
  1.2378 +    }
  1.2379 +  } else {
  1.2380 +    *bp = false;
  1.2381 +
  1.2382 +    return NS_OK;
  1.2383 +  }
  1.2384 +
  1.2385 +  if (name_struct->mType == nsGlobalNameStruct::eTypeExternalConstructorAlias) {
  1.2386 +    name_struct = nameSpaceManager->GetConstructorProto(name_struct);
  1.2387 +    if (!name_struct) {
  1.2388 +      NS_ERROR("Couldn't get constructor prototype.");
  1.2389 +      return NS_ERROR_UNEXPECTED;
  1.2390 +    }
  1.2391 +  }
  1.2392 +
  1.2393 +  NS_ASSERTION(name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor ||
  1.2394 +               name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo,
  1.2395 +               "The constructor was set up with a struct of the wrong type.");
  1.2396 +
  1.2397 +  const nsDOMClassInfoData *ci_data = nullptr;
  1.2398 +  if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor &&
  1.2399 +      name_struct->mDOMClassInfoID >= 0) {
  1.2400 +    ci_data = &sClassInfoData[name_struct->mDOMClassInfoID];
  1.2401 +  } else if (name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.2402 +    ci_data = name_struct->mData;
  1.2403 +  }
  1.2404 +
  1.2405 +  nsCOMPtr<nsIInterfaceInfoManager>
  1.2406 +    iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
  1.2407 +  if (!iim) {
  1.2408 +    NS_ERROR("nsDOMConstructor::HasInstance can't get interface info mgr.");
  1.2409 +    return NS_ERROR_UNEXPECTED;
  1.2410 +  }
  1.2411 +
  1.2412 +  nsCOMPtr<nsIInterfaceInfo> if_info;
  1.2413 +  uint32_t count = 0;
  1.2414 +  const nsIID* class_interface;
  1.2415 +  while ((class_interface = ci_data->mInterfaces[count++])) {
  1.2416 +    if (class_iid->Equals(*class_interface)) {
  1.2417 +      *bp = true;
  1.2418 +
  1.2419 +      return NS_OK;
  1.2420 +    }
  1.2421 +
  1.2422 +    iim->GetInfoForIID(class_interface, getter_AddRefs(if_info));
  1.2423 +    if (!if_info) {
  1.2424 +      NS_ERROR("nsDOMConstructor::HasInstance can't get interface info.");
  1.2425 +      return NS_ERROR_UNEXPECTED;
  1.2426 +    }
  1.2427 +
  1.2428 +    if_info->HasAncestor(class_iid, bp);
  1.2429 +
  1.2430 +    if (*bp) {
  1.2431 +      return NS_OK;
  1.2432 +    }
  1.2433 +  }
  1.2434 +
  1.2435 +  return NS_OK;
  1.2436 +}
  1.2437 +
  1.2438 +nsresult
  1.2439 +nsDOMConstructor::ResolveInterfaceConstants(JSContext *cx, JS::Handle<JSObject*> obj)
  1.2440 +{
  1.2441 +  const nsGlobalNameStruct *class_name_struct = GetNameStruct();
  1.2442 +  if (!class_name_struct)
  1.2443 +    return NS_ERROR_UNEXPECTED;
  1.2444 +
  1.2445 +  const nsIID *class_iid;
  1.2446 +  if (class_name_struct->mType == nsGlobalNameStruct::eTypeInterface ||
  1.2447 +      class_name_struct->mType == nsGlobalNameStruct::eTypeClassProto) {
  1.2448 +    class_iid = &class_name_struct->mIID;
  1.2449 +  } else if (class_name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.2450 +    class_iid =
  1.2451 +      sClassInfoData[class_name_struct->mDOMClassInfoID].mProtoChainInterface;
  1.2452 +  } else if (class_name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.2453 +    class_iid = class_name_struct->mData->mProtoChainInterface;
  1.2454 +  } else {
  1.2455 +    return NS_OK;
  1.2456 +  }
  1.2457 +
  1.2458 +  nsresult rv = DefineInterfaceConstants(cx, obj, class_iid);
  1.2459 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2460 +
  1.2461 +  return NS_OK;
  1.2462 +}
  1.2463 +
  1.2464 +NS_IMETHODIMP
  1.2465 +nsDOMConstructor::ToString(nsAString &aResult)
  1.2466 +{
  1.2467 +  aResult.AssignLiteral("[object ");
  1.2468 +  aResult.Append(mClassName);
  1.2469 +  aResult.Append(char16_t(']'));
  1.2470 +
  1.2471 +  return NS_OK;
  1.2472 +}
  1.2473 +
  1.2474 +
  1.2475 +static nsresult
  1.2476 +GetXPCProto(nsIXPConnect *aXPConnect, JSContext *cx, nsGlobalWindow *aWin,
  1.2477 +            const nsGlobalNameStruct *aNameStruct,
  1.2478 +            nsIXPConnectJSObjectHolder **aProto)
  1.2479 +{
  1.2480 +  NS_ASSERTION(aNameStruct->mType ==
  1.2481 +                 nsGlobalNameStruct::eTypeClassConstructor ||
  1.2482 +               aNameStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo,
  1.2483 +               "Wrong type!");
  1.2484 +
  1.2485 +  nsCOMPtr<nsIClassInfo> ci;
  1.2486 +  if (aNameStruct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.2487 +    int32_t id = aNameStruct->mDOMClassInfoID;
  1.2488 +    NS_ABORT_IF_FALSE(id >= 0, "Negative DOM classinfo?!?");
  1.2489 +
  1.2490 +    nsDOMClassInfoID ci_id = (nsDOMClassInfoID)id;
  1.2491 +
  1.2492 +    ci = NS_GetDOMClassInfoInstance(ci_id);
  1.2493 +
  1.2494 +    // In most cases we want to find the wrapped native prototype in
  1.2495 +    // aWin's scope and use that prototype for
  1.2496 +    // ClassName.prototype. But in the case where we're setting up
  1.2497 +    // "Window.prototype" or "ChromeWindow.prototype" we want to do
  1.2498 +    // the look up in aWin's outer window's scope since the inner
  1.2499 +    // window's wrapped native prototype comes from the outer
  1.2500 +    // window's scope.
  1.2501 +    if (ci_id == eDOMClassInfo_Window_id ||
  1.2502 +        ci_id == eDOMClassInfo_ModalContentWindow_id ||
  1.2503 +        ci_id == eDOMClassInfo_ChromeWindow_id) {
  1.2504 +      nsGlobalWindow *scopeWindow = aWin->GetOuterWindowInternal();
  1.2505 +
  1.2506 +      if (scopeWindow) {
  1.2507 +        aWin = scopeWindow;
  1.2508 +      }
  1.2509 +    }
  1.2510 +  }
  1.2511 +  else {
  1.2512 +    ci = nsDOMClassInfo::GetClassInfoInstance(aNameStruct->mData);
  1.2513 +  }
  1.2514 +  NS_ENSURE_TRUE(ci, NS_ERROR_UNEXPECTED);
  1.2515 +
  1.2516 +  nsresult rv =
  1.2517 +    aXPConnect->GetWrappedNativePrototype(cx, aWin->GetGlobalJSObject(), ci,
  1.2518 +                                          aProto);
  1.2519 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2520 +
  1.2521 +  JS::Rooted<JSObject*> proto_obj(cx, (*aProto)->GetJSObject());
  1.2522 +  if (!JS_WrapObject(cx, &proto_obj)) {
  1.2523 +    return NS_ERROR_FAILURE;
  1.2524 +  }
  1.2525 +
  1.2526 +  NS_IF_RELEASE(*aProto);
  1.2527 +  return aXPConnect->HoldObject(cx, proto_obj, aProto);
  1.2528 +}
  1.2529 +
  1.2530 +// Either ci_data must be non-null or name_struct must be non-null and of type
  1.2531 +// eTypeClassProto.
  1.2532 +static nsresult
  1.2533 +ResolvePrototype(nsIXPConnect *aXPConnect, nsGlobalWindow *aWin, JSContext *cx,
  1.2534 +                 JS::Handle<JSObject*> obj, const char16_t *name,
  1.2535 +                 const nsDOMClassInfoData *ci_data,
  1.2536 +                 const nsGlobalNameStruct *name_struct,
  1.2537 +                 nsScriptNameSpaceManager *nameSpaceManager,
  1.2538 +                 JSObject* aDot_prototype,
  1.2539 +                 JS::MutableHandle<JSPropertyDescriptor> ctorDesc)
  1.2540 +{
  1.2541 +  JS::Rooted<JSObject*> dot_prototype(cx, aDot_prototype);
  1.2542 +  NS_ASSERTION(ci_data ||
  1.2543 +               (name_struct &&
  1.2544 +                name_struct->mType == nsGlobalNameStruct::eTypeClassProto),
  1.2545 +               "Wrong type or missing ci_data!");
  1.2546 +
  1.2547 +  nsRefPtr<nsDOMConstructor> constructor;
  1.2548 +  nsresult rv = nsDOMConstructor::Create(name, ci_data, name_struct, aWin,
  1.2549 +                                         getter_AddRefs(constructor));
  1.2550 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2551 +
  1.2552 +  JS::Rooted<JS::Value> v(cx);
  1.2553 +
  1.2554 +  js::AssertSameCompartment(cx, obj);
  1.2555 +  rv = WrapNative(cx, constructor, &NS_GET_IID(nsIDOMDOMConstructor),
  1.2556 +                  false, &v);
  1.2557 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2558 +
  1.2559 +  FillPropertyDescriptor(ctorDesc, obj, 0, v);
  1.2560 +  // And make sure we wrap the value into the right compartment.  Note that we
  1.2561 +  // do this with ctorDesc.value(), not with v, because we need v to be in the
  1.2562 +  // right compartment (that of the reflector of |constructor|) below.
  1.2563 +  if (!JS_WrapValue(cx, ctorDesc.value())) {
  1.2564 +    return NS_ERROR_UNEXPECTED;
  1.2565 +  }
  1.2566 +
  1.2567 +  JS::Rooted<JSObject*> class_obj(cx, &v.toObject());
  1.2568 +
  1.2569 +  const nsIID *primary_iid = &NS_GET_IID(nsISupports);
  1.2570 +
  1.2571 +  if (!ci_data) {
  1.2572 +    primary_iid = &name_struct->mIID;
  1.2573 +  }
  1.2574 +  else if (ci_data->mProtoChainInterface) {
  1.2575 +    primary_iid = ci_data->mProtoChainInterface;
  1.2576 +  }
  1.2577 +
  1.2578 +  nsCOMPtr<nsIInterfaceInfo> if_info;
  1.2579 +  nsCOMPtr<nsIInterfaceInfo> parent;
  1.2580 +  const char *class_parent_name = nullptr;
  1.2581 +
  1.2582 +  if (!primary_iid->Equals(NS_GET_IID(nsISupports))) {
  1.2583 +    JSAutoCompartment ac(cx, class_obj);
  1.2584 +
  1.2585 +    rv = DefineInterfaceConstants(cx, class_obj, primary_iid);
  1.2586 +    NS_ENSURE_SUCCESS(rv, rv);
  1.2587 +
  1.2588 +    nsCOMPtr<nsIInterfaceInfoManager>
  1.2589 +      iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
  1.2590 +    NS_ENSURE_TRUE(iim, NS_ERROR_NOT_AVAILABLE);
  1.2591 +
  1.2592 +    iim->GetInfoForIID(primary_iid, getter_AddRefs(if_info));
  1.2593 +    NS_ENSURE_TRUE(if_info, NS_ERROR_UNEXPECTED);
  1.2594 +
  1.2595 +    const nsIID *iid = nullptr;
  1.2596 +
  1.2597 +    if (ci_data && !ci_data->mHasClassInterface) {
  1.2598 +      if_info->GetIIDShared(&iid);
  1.2599 +    } else {
  1.2600 +      if_info->GetParent(getter_AddRefs(parent));
  1.2601 +      NS_ENSURE_TRUE(parent, NS_ERROR_UNEXPECTED);
  1.2602 +
  1.2603 +      parent->GetIIDShared(&iid);
  1.2604 +    }
  1.2605 +
  1.2606 +    if (iid) {
  1.2607 +      if (!iid->Equals(NS_GET_IID(nsISupports))) {
  1.2608 +        if (ci_data && !ci_data->mHasClassInterface) {
  1.2609 +          // If the class doesn't have a class interface the primary
  1.2610 +          // interface is the interface that should be
  1.2611 +          // constructor.prototype.__proto__.
  1.2612 +
  1.2613 +          if_info->GetNameShared(&class_parent_name);
  1.2614 +        } else {
  1.2615 +          // If the class does have a class interface (or there's no
  1.2616 +          // real class for this name) then the parent of the
  1.2617 +          // primary interface is what we want on
  1.2618 +          // constructor.prototype.__proto__.
  1.2619 +
  1.2620 +          NS_ASSERTION(parent, "Whoa, this is bad, null parent here!");
  1.2621 +
  1.2622 +          parent->GetNameShared(&class_parent_name);
  1.2623 +        }
  1.2624 +      }
  1.2625 +    }
  1.2626 +  }
  1.2627 +
  1.2628 +  {
  1.2629 +    JS::Rooted<JSObject*> winobj(cx, aWin->FastGetGlobalJSObject());
  1.2630 +
  1.2631 +    JS::Rooted<JSObject*> proto(cx);
  1.2632 +
  1.2633 +    if (class_parent_name) {
  1.2634 +      JSAutoCompartment ac(cx, winobj);
  1.2635 +
  1.2636 +      JS::Rooted<JS::Value> val(cx);
  1.2637 +      if (!JS_LookupProperty(cx, winobj, CutPrefix(class_parent_name), &val)) {
  1.2638 +        return NS_ERROR_UNEXPECTED;
  1.2639 +      }
  1.2640 +
  1.2641 +      if (val.isObject()) {
  1.2642 +        JS::Rooted<JSObject*> obj(cx, &val.toObject());
  1.2643 +        if (!JS_LookupProperty(cx, obj, "prototype", &val)) {
  1.2644 +          return NS_ERROR_UNEXPECTED;
  1.2645 +        }
  1.2646 +
  1.2647 +        if (val.isObject()) {
  1.2648 +          proto = &val.toObject();
  1.2649 +        }
  1.2650 +      }
  1.2651 +    }
  1.2652 +
  1.2653 +    if (dot_prototype) {
  1.2654 +      JSAutoCompartment ac(cx, dot_prototype);
  1.2655 +      JS::Rooted<JSObject*> xpc_proto_proto(cx);
  1.2656 +      if (!::JS_GetPrototype(cx, dot_prototype, &xpc_proto_proto)) {
  1.2657 +        return NS_ERROR_UNEXPECTED;
  1.2658 +      }
  1.2659 +
  1.2660 +      if (proto &&
  1.2661 +          (!xpc_proto_proto ||
  1.2662 +           JS_GetClass(xpc_proto_proto) == sObjectClass)) {
  1.2663 +        if (!JS_WrapObject(cx, &proto) ||
  1.2664 +            !JS_SetPrototype(cx, dot_prototype, proto)) {
  1.2665 +          return NS_ERROR_UNEXPECTED;
  1.2666 +        }
  1.2667 +      }
  1.2668 +    } else {
  1.2669 +      JSAutoCompartment ac(cx, winobj);
  1.2670 +      if (!proto) {
  1.2671 +        proto = JS_GetObjectPrototype(cx, winobj);
  1.2672 +      }
  1.2673 +      dot_prototype = ::JS_NewObjectWithUniqueType(cx,
  1.2674 +                                                   &sDOMConstructorProtoClass,
  1.2675 +                                                   proto,
  1.2676 +                                                   winobj);
  1.2677 +      NS_ENSURE_TRUE(dot_prototype, NS_ERROR_OUT_OF_MEMORY);
  1.2678 +    }
  1.2679 +  }
  1.2680 +
  1.2681 +  v = OBJECT_TO_JSVAL(dot_prototype);
  1.2682 +
  1.2683 +  JSAutoCompartment ac(cx, class_obj);
  1.2684 +
  1.2685 +  // Per ECMA, the prototype property is {DontEnum, DontDelete, ReadOnly}
  1.2686 +  if (!JS_WrapValue(cx, &v) ||
  1.2687 +      !JS_DefineProperty(cx, class_obj, "prototype", v,
  1.2688 +                         JSPROP_PERMANENT | JSPROP_READONLY,
  1.2689 +                         JS_PropertyStub, JS_StrictPropertyStub)) {
  1.2690 +    return NS_ERROR_UNEXPECTED;
  1.2691 +  }
  1.2692 +
  1.2693 +  return NS_OK;
  1.2694 +}
  1.2695 +
  1.2696 +static bool
  1.2697 +OldBindingConstructorEnabled(const nsGlobalNameStruct *aStruct,
  1.2698 +                             nsGlobalWindow *aWin, JSContext *cx)
  1.2699 +{
  1.2700 +  MOZ_ASSERT(aStruct->mType == nsGlobalNameStruct::eTypeProperty ||
  1.2701 +             aStruct->mType == nsGlobalNameStruct::eTypeClassConstructor ||
  1.2702 +             aStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo);
  1.2703 +
  1.2704 +  // Don't expose chrome only constructors to content windows.
  1.2705 +  if (aStruct->mChromeOnly) {
  1.2706 +    bool expose;
  1.2707 +    if (aStruct->mAllowXBL) {
  1.2708 +      expose = IsChromeOrXBL(cx, nullptr);
  1.2709 +    } else {
  1.2710 +      expose = nsContentUtils::IsSystemPrincipal(aWin->GetPrincipal());
  1.2711 +    }
  1.2712 +
  1.2713 +    if (!expose) {
  1.2714 +      return false;
  1.2715 +    }
  1.2716 +  }
  1.2717 +
  1.2718 +  // Don't expose CSSSupportsRule unless @supports processing is enabled.
  1.2719 +  if (aStruct->mDOMClassInfoID == eDOMClassInfo_CSSSupportsRule_id) {
  1.2720 +    if (!CSSSupportsRule::PrefEnabled()) {
  1.2721 +      return false;
  1.2722 +    }
  1.2723 +  }
  1.2724 +
  1.2725 +  // Don't expose CSSFontFeatureValuesRule unless the pref is enabled
  1.2726 +  if (aStruct->mDOMClassInfoID == eDOMClassInfo_CSSFontFeatureValuesRule_id) {
  1.2727 +    return nsCSSFontFeatureValuesRule::PrefEnabled();
  1.2728 +  }
  1.2729 +
  1.2730 +  return true;
  1.2731 +}
  1.2732 +
  1.2733 +bool
  1.2734 +nsWindowSH::NameStructEnabled(JSContext* aCx, nsGlobalWindow *aWin,
  1.2735 +                              const nsAString& aName,
  1.2736 +                              const nsGlobalNameStruct& aNameStruct)
  1.2737 +{
  1.2738 +  const nsGlobalNameStruct* nameStruct = &aNameStruct;
  1.2739 +  if (nameStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
  1.2740 +    nsresult rv = GetExternalClassInfo(GetNameSpaceManager(), aName, nameStruct,
  1.2741 +                                       &nameStruct);
  1.2742 +    if (NS_FAILED(rv) || !nameStruct) {
  1.2743 +      return false;
  1.2744 +    }
  1.2745 +  }
  1.2746 +
  1.2747 +  return (nameStruct->mType != nsGlobalNameStruct::eTypeProperty &&
  1.2748 +          nameStruct->mType != nsGlobalNameStruct::eTypeClassConstructor &&
  1.2749 +          nameStruct->mType != nsGlobalNameStruct::eTypeExternalClassInfo) ||
  1.2750 +         OldBindingConstructorEnabled(nameStruct, aWin, aCx);
  1.2751 +}
  1.2752 +
  1.2753 +#ifdef RELEASE_BUILD
  1.2754 +#define USE_CONTROLLERS_SHIM
  1.2755 +#endif
  1.2756 +
  1.2757 +#ifdef USE_CONTROLLERS_SHIM
  1.2758 +static const JSClass ControllersShimClass = {
  1.2759 +    "XULControllers", 0,
  1.2760 +    JS_PropertyStub,   JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
  1.2761 +    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, nullptr
  1.2762 +};
  1.2763 +#endif
  1.2764 +
  1.2765 +// static
  1.2766 +nsresult
  1.2767 +nsWindowSH::GlobalResolve(nsGlobalWindow *aWin, JSContext *cx,
  1.2768 +                          JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
  1.2769 +                          JS::MutableHandle<JSPropertyDescriptor> desc)
  1.2770 +{
  1.2771 +#ifdef USE_CONTROLLERS_SHIM
  1.2772 +  if (id == XPCJSRuntime::Get()->GetStringID(XPCJSRuntime::IDX_CONTROLLERS) &&
  1.2773 +      !xpc::IsXrayWrapper(obj) &&
  1.2774 +      !nsContentUtils::IsSystemPrincipal(aWin->GetPrincipal()))
  1.2775 +  {
  1.2776 +    if (aWin->GetDoc()) {
  1.2777 +      aWin->GetDoc()->WarnOnceAbout(nsIDocument::eWindow_Controllers);
  1.2778 +    }
  1.2779 +    JS::Rooted<JSObject*> shim(cx, JS_NewObject(cx, &ControllersShimClass, JS::NullPtr(), obj));
  1.2780 +    if (NS_WARN_IF(!shim)) {
  1.2781 +      return NS_ERROR_OUT_OF_MEMORY;
  1.2782 +    }
  1.2783 +    FillPropertyDescriptor(desc, obj, JS::ObjectValue(*shim), /* readOnly = */ false);
  1.2784 +    return NS_OK;
  1.2785 +  }
  1.2786 +#endif
  1.2787 +
  1.2788 +  nsScriptNameSpaceManager *nameSpaceManager = GetNameSpaceManager();
  1.2789 +  NS_ENSURE_TRUE(nameSpaceManager, NS_ERROR_NOT_INITIALIZED);
  1.2790 +
  1.2791 +  nsDependentJSString name(id);
  1.2792 +
  1.2793 +  const char16_t *class_name = nullptr;
  1.2794 +  const nsGlobalNameStruct *name_struct =
  1.2795 +    nameSpaceManager->LookupName(name, &class_name);
  1.2796 +
  1.2797 +  if (!name_struct) {
  1.2798 +    return NS_OK;
  1.2799 +  }
  1.2800 +
  1.2801 +  // The class_name had better match our name
  1.2802 +  MOZ_ASSERT(name.Equals(class_name));
  1.2803 +
  1.2804 +  NS_ENSURE_TRUE(class_name, NS_ERROR_UNEXPECTED);
  1.2805 +
  1.2806 +  nsresult rv = NS_OK;
  1.2807 +
  1.2808 +  if (name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
  1.2809 +    rv = GetExternalClassInfo(nameSpaceManager, name, name_struct,
  1.2810 +                              &name_struct);
  1.2811 +    if (NS_FAILED(rv) || !name_struct) {
  1.2812 +      return rv;
  1.2813 +    }
  1.2814 +  }
  1.2815 +
  1.2816 +  if (name_struct->mType == nsGlobalNameStruct::eTypeNewDOMBinding ||
  1.2817 +      name_struct->mType == nsGlobalNameStruct::eTypeInterface ||
  1.2818 +      name_struct->mType == nsGlobalNameStruct::eTypeClassProto ||
  1.2819 +      name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.2820 +    // Lookup new DOM bindings.
  1.2821 +    DefineInterface getOrCreateInterfaceObject =
  1.2822 +      name_struct->mDefineDOMInterface;
  1.2823 +    if (getOrCreateInterfaceObject) {
  1.2824 +      if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor &&
  1.2825 +          !OldBindingConstructorEnabled(name_struct, aWin, cx)) {
  1.2826 +        return NS_OK;
  1.2827 +      }
  1.2828 +
  1.2829 +      ConstructorEnabled* checkEnabledForScope = name_struct->mConstructorEnabled;
  1.2830 +      // We do the enabled check on the current compartment of cx, but for the
  1.2831 +      // actual object we pass in the underlying object in the Xray case.  That
  1.2832 +      // way the callee can decide whether to allow access based on the caller
  1.2833 +      // or the window being touched.
  1.2834 +      JS::Rooted<JSObject*> global(cx,
  1.2835 +        js::CheckedUnwrap(obj, /* stopAtOuter = */ false));
  1.2836 +      if (!global) {
  1.2837 +        return NS_ERROR_DOM_SECURITY_ERR;
  1.2838 +      }
  1.2839 +      if (checkEnabledForScope && !checkEnabledForScope(cx, global)) {
  1.2840 +        return NS_OK;
  1.2841 +      }
  1.2842 +
  1.2843 +      // The DOM constructor resolve machinery interacts with Xrays in tricky
  1.2844 +      // ways, and there are some asymmetries that are important to understand.
  1.2845 +      //
  1.2846 +      // In the regular (non-Xray) case, we only want to resolve constructors
  1.2847 +      // once (so that if they're deleted, they don't reappear). We do this by
  1.2848 +      // stashing the constructor in a slot on the global, such that we can see
  1.2849 +      // during resolve whether we've created it already. This is rather
  1.2850 +      // memory-intensive, so we don't try to maintain these semantics when
  1.2851 +      // manipulating a global over Xray (so the properties just re-resolve if
  1.2852 +      // they've been deleted).
  1.2853 +      //
  1.2854 +      // Unfortunately, there's a bit of an impedance-mismatch between the Xray
  1.2855 +      // and non-Xray machinery. The Xray machinery wants an API that returns a
  1.2856 +      // JSPropertyDescriptor, so that the resolve hook doesn't have to get
  1.2857 +      // snared up with trying to define a property on the Xray holder. At the
  1.2858 +      // same time, the DefineInterface callbacks are set up to define things
  1.2859 +      // directly on the global.  And re-jiggering them to return property
  1.2860 +      // descriptors is tricky, because some DefineInterface callbacks define
  1.2861 +      // multiple things (like the Image() alias for HTMLImageElement).
  1.2862 +      //
  1.2863 +      // So the setup is as-follows:
  1.2864 +      //
  1.2865 +      // * The resolve function takes a JSPropertyDescriptor, but in the
  1.2866 +      //   non-Xray case, callees may define things directly on the global, and
  1.2867 +      //   set the value on the property descriptor to |undefined| to indicate
  1.2868 +      //   that there's nothing more for the caller to do. We assert against
  1.2869 +      //   this behavior in the Xray case.
  1.2870 +      //
  1.2871 +      // * We make sure that we do a non-Xray resolve first, so that all the
  1.2872 +      //   slots are set up. In the Xray case, this means unwrapping and doing
  1.2873 +      //   a non-Xray resolve before doing the Xray resolve.
  1.2874 +      //
  1.2875 +      // This all could use some grand refactoring, but for now we just limp
  1.2876 +      // along.
  1.2877 +      if (xpc::WrapperFactory::IsXrayWrapper(obj)) {
  1.2878 +        JS::Rooted<JSObject*> interfaceObject(cx);
  1.2879 +        {
  1.2880 +          JSAutoCompartment ac(cx, global);
  1.2881 +          interfaceObject = getOrCreateInterfaceObject(cx, global, id, false);
  1.2882 +        }
  1.2883 +        if (NS_WARN_IF(!interfaceObject)) {
  1.2884 +          return NS_ERROR_FAILURE;
  1.2885 +        }
  1.2886 +        if (!JS_WrapObject(cx, &interfaceObject)) {
  1.2887 +          return NS_ERROR_FAILURE;
  1.2888 +        }
  1.2889 +
  1.2890 +        FillPropertyDescriptor(desc, obj, 0, JS::ObjectValue(*interfaceObject));
  1.2891 +      } else {
  1.2892 +        JS::Rooted<JSObject*> interfaceObject(cx,
  1.2893 +          getOrCreateInterfaceObject(cx, obj, id, true));
  1.2894 +        if (NS_WARN_IF(!interfaceObject)) {
  1.2895 +          return NS_ERROR_FAILURE;
  1.2896 +        }
  1.2897 +        // We've already defined the property.  We indicate this to the caller
  1.2898 +        // by filling a property descriptor with JS::UndefinedValue() as the
  1.2899 +        // value.  We still have to fill in a property descriptor, though, so
  1.2900 +        // that the caller knows the property is in fact on this object.  It
  1.2901 +        // doesn't matter what we pass for the "readonly" argument here.
  1.2902 +        FillPropertyDescriptor(desc, obj, JS::UndefinedValue(), false);
  1.2903 +      }
  1.2904 +
  1.2905 +      return NS_OK;
  1.2906 +    }
  1.2907 +  }
  1.2908 +
  1.2909 +  if (name_struct->mType == nsGlobalNameStruct::eTypeInterface) {
  1.2910 +    // We're resolving a name of a DOM interface for which there is no
  1.2911 +    // direct DOM class, create a constructor object...
  1.2912 +    nsRefPtr<nsDOMConstructor> constructor;
  1.2913 +    rv = nsDOMConstructor::Create(class_name,
  1.2914 +                                  nullptr,
  1.2915 +                                  name_struct,
  1.2916 +                                  static_cast<nsPIDOMWindow*>(aWin),
  1.2917 +                                  getter_AddRefs(constructor));
  1.2918 +    NS_ENSURE_SUCCESS(rv, rv);
  1.2919 +
  1.2920 +    JS::Rooted<JS::Value> v(cx);
  1.2921 +    js::AssertSameCompartment(cx, obj);
  1.2922 +    rv = WrapNative(cx, constructor, &NS_GET_IID(nsIDOMDOMConstructor),
  1.2923 +                    false, &v);
  1.2924 +    NS_ENSURE_SUCCESS(rv, rv);
  1.2925 +
  1.2926 +    JS::Rooted<JSObject*> class_obj(cx, &v.toObject());
  1.2927 +
  1.2928 +    // ... and define the constants from the DOM interface on that
  1.2929 +    // constructor object.
  1.2930 +
  1.2931 +    {
  1.2932 +      JSAutoCompartment ac(cx, class_obj);
  1.2933 +      rv = DefineInterfaceConstants(cx, class_obj, &name_struct->mIID);
  1.2934 +      NS_ENSURE_SUCCESS(rv, rv);
  1.2935 +    }
  1.2936 +
  1.2937 +    if (!JS_WrapValue(cx, &v)) {
  1.2938 +      return NS_ERROR_UNEXPECTED;
  1.2939 +    }
  1.2940 +
  1.2941 +    FillPropertyDescriptor(desc, obj, 0, v);
  1.2942 +    return NS_OK;
  1.2943 +  }
  1.2944 +
  1.2945 +  if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor ||
  1.2946 +      name_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.2947 +    if (!OldBindingConstructorEnabled(name_struct, aWin, cx)) {
  1.2948 +      return NS_OK;
  1.2949 +    }
  1.2950 +
  1.2951 +    // Create the XPConnect prototype for our classinfo, PostCreateProto will
  1.2952 +    // set up the prototype chain.  This will go ahead and define things on the
  1.2953 +    // actual window's global.
  1.2954 +    nsCOMPtr<nsIXPConnectJSObjectHolder> proto_holder;
  1.2955 +    rv = GetXPCProto(sXPConnect, cx, aWin, name_struct,
  1.2956 +                     getter_AddRefs(proto_holder));
  1.2957 +    NS_ENSURE_SUCCESS(rv, rv);
  1.2958 +    bool isXray = xpc::WrapperFactory::IsXrayWrapper(obj);
  1.2959 +    MOZ_ASSERT_IF(obj != aWin->GetGlobalJSObject(), isXray);
  1.2960 +    if (!isXray) {
  1.2961 +      // GetXPCProto already defined the property for us
  1.2962 +      FillPropertyDescriptor(desc, obj, JS::UndefinedValue(), false);
  1.2963 +      return NS_OK;
  1.2964 +    }
  1.2965 +
  1.2966 +    // This is the Xray case.  Look up the constructor object for this
  1.2967 +    // prototype.
  1.2968 +    JS::Rooted<JSObject*> dot_prototype(cx, proto_holder->GetJSObject());
  1.2969 +    NS_ENSURE_STATE(dot_prototype);
  1.2970 +
  1.2971 +    const nsDOMClassInfoData *ci_data;
  1.2972 +    if (name_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.2973 +      ci_data = &sClassInfoData[name_struct->mDOMClassInfoID];
  1.2974 +    } else {
  1.2975 +      ci_data = name_struct->mData;
  1.2976 +    }
  1.2977 +
  1.2978 +    return ResolvePrototype(sXPConnect, aWin, cx, obj, class_name, ci_data,
  1.2979 +                            name_struct, nameSpaceManager, dot_prototype,
  1.2980 +                            desc);
  1.2981 +  }
  1.2982 +
  1.2983 +  if (name_struct->mType == nsGlobalNameStruct::eTypeClassProto) {
  1.2984 +    // We don't have a XPConnect prototype object, let ResolvePrototype create
  1.2985 +    // one.
  1.2986 +    return ResolvePrototype(sXPConnect, aWin, cx, obj, class_name, nullptr,
  1.2987 +                            name_struct, nameSpaceManager, nullptr, desc);
  1.2988 +  }
  1.2989 +
  1.2990 +  if (name_struct->mType == nsGlobalNameStruct::eTypeExternalConstructorAlias) {
  1.2991 +    const nsGlobalNameStruct *alias_struct =
  1.2992 +      nameSpaceManager->GetConstructorProto(name_struct);
  1.2993 +    NS_ENSURE_TRUE(alias_struct, NS_ERROR_UNEXPECTED);
  1.2994 +
  1.2995 +    // We need to use the XPConnect prototype for the DOM class that this
  1.2996 +    // constructor is an alias for (for example for Image we need the prototype
  1.2997 +    // for HTMLImageElement).
  1.2998 +    nsCOMPtr<nsIXPConnectJSObjectHolder> proto_holder;
  1.2999 +    rv = GetXPCProto(sXPConnect, cx, aWin, alias_struct,
  1.3000 +                     getter_AddRefs(proto_holder));
  1.3001 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3002 +
  1.3003 +    JSObject* dot_prototype = proto_holder->GetJSObject();
  1.3004 +    NS_ENSURE_STATE(dot_prototype);
  1.3005 +
  1.3006 +    const nsDOMClassInfoData *ci_data;
  1.3007 +    if (alias_struct->mType == nsGlobalNameStruct::eTypeClassConstructor) {
  1.3008 +      ci_data = &sClassInfoData[alias_struct->mDOMClassInfoID];
  1.3009 +    } else if (alias_struct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
  1.3010 +      ci_data = alias_struct->mData;
  1.3011 +    } else {
  1.3012 +      return NS_ERROR_UNEXPECTED;
  1.3013 +    }
  1.3014 +
  1.3015 +    return ResolvePrototype(sXPConnect, aWin, cx, obj, class_name, ci_data,
  1.3016 +                            name_struct, nameSpaceManager, nullptr, desc);
  1.3017 +  }
  1.3018 +
  1.3019 +  if (name_struct->mType == nsGlobalNameStruct::eTypeExternalConstructor) {
  1.3020 +    nsRefPtr<nsDOMConstructor> constructor;
  1.3021 +    rv = nsDOMConstructor::Create(class_name, nullptr, name_struct,
  1.3022 +                                  static_cast<nsPIDOMWindow*>(aWin),
  1.3023 +                                  getter_AddRefs(constructor));
  1.3024 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3025 +
  1.3026 +    JS::Rooted<JS::Value> val(cx);
  1.3027 +    js::AssertSameCompartment(cx, obj);
  1.3028 +    rv = WrapNative(cx, constructor, &NS_GET_IID(nsIDOMDOMConstructor),
  1.3029 +                    true, &val);
  1.3030 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3031 +
  1.3032 +    NS_ASSERTION(val.isObject(), "Why didn't we get a JSObject?");
  1.3033 +
  1.3034 +    FillPropertyDescriptor(desc, obj, 0, val);
  1.3035 +
  1.3036 +    return NS_OK;
  1.3037 +  }
  1.3038 +
  1.3039 +  if (name_struct->mType == nsGlobalNameStruct::eTypeProperty) {
  1.3040 +    if (!OldBindingConstructorEnabled(name_struct, aWin, cx))
  1.3041 +      return NS_OK;
  1.3042 +
  1.3043 +    // Before defining a global property, check for a named subframe of the
  1.3044 +    // same name. If it exists, we don't want to shadow it.
  1.3045 +    nsCOMPtr<nsIDOMWindow> childWin = aWin->GetChildWindow(name);
  1.3046 +    if (childWin)
  1.3047 +      return NS_OK;
  1.3048 +
  1.3049 +    nsCOMPtr<nsISupports> native(do_CreateInstance(name_struct->mCID, &rv));
  1.3050 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3051 +
  1.3052 +    JS::Rooted<JS::Value> prop_val(cx, JS::UndefinedValue()); // Property value.
  1.3053 +
  1.3054 +    nsCOMPtr<nsIDOMGlobalPropertyInitializer> gpi(do_QueryInterface(native));
  1.3055 +    if (gpi) {
  1.3056 +      rv = gpi->Init(aWin, &prop_val);
  1.3057 +      NS_ENSURE_SUCCESS(rv, rv);
  1.3058 +    }
  1.3059 +
  1.3060 +    if (JSVAL_IS_PRIMITIVE(prop_val) && !JSVAL_IS_NULL(prop_val)) {
  1.3061 +      if (aWin->IsOuterWindow()) {
  1.3062 +        nsGlobalWindow *inner = aWin->GetCurrentInnerWindowInternal();
  1.3063 +        NS_ENSURE_TRUE(inner, NS_ERROR_UNEXPECTED);
  1.3064 +      }
  1.3065 +
  1.3066 +      rv = WrapNative(cx, native, true, &prop_val);
  1.3067 +    }
  1.3068 +
  1.3069 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3070 +
  1.3071 +    if (!JS_WrapValue(cx, &prop_val)) {
  1.3072 +      return NS_ERROR_UNEXPECTED;
  1.3073 +    }
  1.3074 +
  1.3075 +    FillPropertyDescriptor(desc, obj, prop_val, false);
  1.3076 +
  1.3077 +    return NS_OK;
  1.3078 +  }
  1.3079 +
  1.3080 +  return rv;
  1.3081 +}
  1.3082 +
  1.3083 +template<class Interface>
  1.3084 +static nsresult
  1.3085 +LocationSetterGuts(JSContext *cx, JSObject *obj, JS::MutableHandle<JS::Value> vp)
  1.3086 +{
  1.3087 +  // This function duplicates some of the logic in XPC_WN_HelperSetProperty
  1.3088 +  obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
  1.3089 +  if (!IS_WN_REFLECTOR(obj))
  1.3090 +      return NS_ERROR_XPC_BAD_CONVERT_JS;
  1.3091 +  XPCWrappedNative *wrapper = XPCWrappedNative::Get(obj);
  1.3092 +
  1.3093 +  // The error checks duplicate code in THROW_AND_RETURN_IF_BAD_WRAPPER
  1.3094 +  NS_ENSURE_TRUE(!wrapper || wrapper->IsValid(), NS_ERROR_XPC_HAS_BEEN_SHUTDOWN);
  1.3095 +
  1.3096 +  nsCOMPtr<Interface> xpcomObj = do_QueryWrappedNative(wrapper, obj);
  1.3097 +  NS_ENSURE_TRUE(xpcomObj, NS_ERROR_UNEXPECTED);
  1.3098 +
  1.3099 +  nsCOMPtr<nsIDOMLocation> location;
  1.3100 +  nsresult rv = xpcomObj->GetLocation(getter_AddRefs(location));
  1.3101 +  NS_ENSURE_SUCCESS(rv, rv);
  1.3102 +
  1.3103 +  // Grab the value we're being set to before we stomp on |vp|
  1.3104 +  JS::Rooted<JSString*> val(cx, JS::ToString(cx, vp));
  1.3105 +  NS_ENSURE_TRUE(val, NS_ERROR_UNEXPECTED);
  1.3106 +
  1.3107 +  // Make sure |val| stays alive below
  1.3108 +  JS::Anchor<JSString *> anchor(val);
  1.3109 +
  1.3110 +  // We have to wrap location into vp before null-checking location, to
  1.3111 +  // avoid assigning the wrong thing into the slot.
  1.3112 +  rv = WrapNative(cx, location, &NS_GET_IID(nsIDOMLocation), true, vp);
  1.3113 +  NS_ENSURE_SUCCESS(rv, rv);
  1.3114 +
  1.3115 +  if (!location) {
  1.3116 +    // Make this a no-op
  1.3117 +    return NS_OK;
  1.3118 +  }
  1.3119 +
  1.3120 +  nsDependentJSString depStr;
  1.3121 +  NS_ENSURE_TRUE(depStr.init(cx, val), NS_ERROR_UNEXPECTED);
  1.3122 +
  1.3123 +  return location->SetHref(depStr);
  1.3124 +}
  1.3125 +
  1.3126 +template<class Interface>
  1.3127 +static bool
  1.3128 +LocationSetter(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, bool strict,
  1.3129 +               JS::MutableHandle<JS::Value> vp)
  1.3130 +{
  1.3131 +  nsresult rv = LocationSetterGuts<Interface>(cx, obj, vp);
  1.3132 +  if (NS_FAILED(rv)) {
  1.3133 +    xpc::Throw(cx, rv);
  1.3134 +    return false;
  1.3135 +  }
  1.3136 +
  1.3137 +  return true;
  1.3138 +}
  1.3139 +
  1.3140 +static bool
  1.3141 +LocationSetterUnwrapper(JSContext *cx, JS::Handle<JSObject*> obj_, JS::Handle<jsid> id,
  1.3142 +                        bool strict, JS::MutableHandle<JS::Value> vp)
  1.3143 +{
  1.3144 +  JS::Rooted<JSObject*> obj(cx, obj_);
  1.3145 +
  1.3146 +  JSObject *wrapped = XPCWrapper::UnsafeUnwrapSecurityWrapper(obj);
  1.3147 +  if (wrapped) {
  1.3148 +    obj = wrapped;
  1.3149 +  }
  1.3150 +
  1.3151 +  return LocationSetter<nsIDOMWindow>(cx, obj, id, strict, vp);
  1.3152 +}
  1.3153 +
  1.3154 +NS_IMETHODIMP
  1.3155 +nsWindowSH::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3156 +                       JSObject *obj_, jsid id_, JSObject **objp,
  1.3157 +                       bool *_retval)
  1.3158 +{
  1.3159 +  JS::Rooted<JSObject*> obj(cx, obj_);
  1.3160 +  JS::Rooted<jsid> id(cx, id_);
  1.3161 +
  1.3162 +  if (!JSID_IS_STRING(id)) {
  1.3163 +    return NS_OK;
  1.3164 +  }
  1.3165 +
  1.3166 +  MOZ_ASSERT(*_retval == true); // guaranteed by XPC_WN_Helper_NewResolve
  1.3167 +
  1.3168 +  nsGlobalWindow *win = nsGlobalWindow::FromWrapper(wrapper);
  1.3169 +  MOZ_ASSERT(win->IsInnerWindow());
  1.3170 +
  1.3171 +  // Don't resolve standard classes on XrayWrappers, only resolve them if we're
  1.3172 +  // resolving on the real global object.
  1.3173 +  bool isXray = xpc::WrapperFactory::IsXrayWrapper(obj);
  1.3174 +  if (!isXray) {
  1.3175 +    bool did_resolve = false;
  1.3176 +    if (!JS_ResolveStandardClass(cx, obj, id, &did_resolve)) {
  1.3177 +      // Return NS_OK to avoid stomping over the exception that was passed
  1.3178 +      // down from the ResolveStandardClass call.
  1.3179 +      *_retval = false;
  1.3180 +      return NS_OK;
  1.3181 +    }
  1.3182 +
  1.3183 +    if (did_resolve) {
  1.3184 +      *objp = obj;
  1.3185 +      return NS_OK;
  1.3186 +    }
  1.3187 +  }
  1.3188 +
  1.3189 +  // WebIDL quickstubs handle location for us, but Xrays don't see those.  So if
  1.3190 +  // we're an Xray, we have to resolve stuff here to make "window.location =
  1.3191 +  // someString" work.
  1.3192 +  if (sLocation_id == id && isXray) {
  1.3193 +    nsCOMPtr<nsIDOMLocation> location;
  1.3194 +    nsresult rv = win->GetLocation(getter_AddRefs(location));
  1.3195 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3196 +
  1.3197 +    JS::Rooted<JS::Value> v(cx);
  1.3198 +    rv = WrapNative(cx, location, &NS_GET_IID(nsIDOMLocation), true, &v);
  1.3199 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3200 +
  1.3201 +    bool ok = JS_DefinePropertyById(cx, obj, id, v, JS_PropertyStub,
  1.3202 +                                    LocationSetterUnwrapper,
  1.3203 +                                    JSPROP_PERMANENT | JSPROP_ENUMERATE);
  1.3204 +
  1.3205 +    if (!ok) {
  1.3206 +      return NS_ERROR_FAILURE;
  1.3207 +    }
  1.3208 +
  1.3209 +    *objp = obj;
  1.3210 +
  1.3211 +    return NS_OK;
  1.3212 +  }
  1.3213 +
  1.3214 +  // WebIDL quickstubs handle "top" for us, but Xrays don't see those.  So if
  1.3215 +  // we're an Xray and we want "top" to be JSPROP_PERMANENT, we need to resolve
  1.3216 +  // it here.
  1.3217 +  if (sTop_id == id && isXray) {
  1.3218 +    nsCOMPtr<nsIDOMWindow> top;
  1.3219 +    nsresult rv = win->GetScriptableTop(getter_AddRefs(top));
  1.3220 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3221 +
  1.3222 +    JS::Rooted<JS::Value> v(cx);
  1.3223 +    js::AssertSameCompartment(cx, obj);
  1.3224 +    rv = WrapNative(cx, top, &NS_GET_IID(nsIDOMWindow), true, &v);
  1.3225 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3226 +
  1.3227 +    // Hold on to the top window object as a global property so we
  1.3228 +    // don't need to worry about losing expando properties etc.
  1.3229 +    if (!JS_DefinePropertyById(cx, obj, id, v, JS_PropertyStub, JS_StrictPropertyStub,
  1.3230 +                               JSPROP_READONLY | JSPROP_PERMANENT |
  1.3231 +                               JSPROP_ENUMERATE)) {
  1.3232 +      return NS_ERROR_FAILURE;
  1.3233 +    }
  1.3234 +    *objp = obj;
  1.3235 +
  1.3236 +    return NS_OK;
  1.3237 +  }
  1.3238 +
  1.3239 +  if (isXray) {
  1.3240 +    // We promise to resolve on the underlying object first.  That will create
  1.3241 +    // the actual interface object if needed and store it in a data structure
  1.3242 +    // hanging off the global.  Then our second call will wrap up in an Xray as
  1.3243 +    // needed.  We do things this way because we use the existence of the
  1.3244 +    // object in that data structure as a flag that indicates that its name
  1.3245 +    // (and any relevant named constructor names) has been resolved before;
  1.3246 +    // this allows us to avoid re-resolving in the Xray case if the property is
  1.3247 +    // deleted by page script.
  1.3248 +    JS::Rooted<JSObject*> global(cx,
  1.3249 +      js::UncheckedUnwrap(obj, /* stopAtOuter = */ false));
  1.3250 +    JSAutoCompartment ac(cx, global);
  1.3251 +    JS::Rooted<JSPropertyDescriptor> desc(cx);
  1.3252 +    if (!win->DoNewResolve(cx, global, id, &desc)) {
  1.3253 +      return NS_ERROR_FAILURE;
  1.3254 +    }
  1.3255 +    // If we have an object here, that means we resolved the property.
  1.3256 +    // But if the value is undefined, that means that GlobalResolve
  1.3257 +    // also already defined it, so we don't have to.
  1.3258 +    if (desc.object() && !desc.value().isUndefined() &&
  1.3259 +        !JS_DefinePropertyById(cx, global, id, desc.value(),
  1.3260 +                               desc.getter(), desc.setter(),
  1.3261 +                               desc.attributes())) {
  1.3262 +      return NS_ERROR_FAILURE;
  1.3263 +    }
  1.3264 +  }
  1.3265 +
  1.3266 +  JS::Rooted<JSPropertyDescriptor> desc(cx);
  1.3267 +  if (!win->DoNewResolve(cx, obj, id, &desc)) {
  1.3268 +    return NS_ERROR_FAILURE;
  1.3269 +  }
  1.3270 +  if (desc.object()) {
  1.3271 +    // If we have an object here, that means we resolved the property.
  1.3272 +    // But if the value is undefined, that means that GlobalResolve
  1.3273 +    // also already defined it, so we don't have to.  Note that in the
  1.3274 +    // Xray case we should never see undefined.
  1.3275 +    MOZ_ASSERT_IF(isXray, !desc.value().isUndefined());
  1.3276 +    if (!desc.value().isUndefined() &&
  1.3277 +        !JS_DefinePropertyById(cx, obj, id, desc.value(),
  1.3278 +                               desc.getter(), desc.setter(),
  1.3279 +                               desc.attributes())) {
  1.3280 +      return NS_ERROR_FAILURE;
  1.3281 +    }
  1.3282 +
  1.3283 +    *objp = obj;
  1.3284 +    return NS_OK;
  1.3285 +  }
  1.3286 +
  1.3287 +  if (sDocument_id == id) {
  1.3288 +    nsCOMPtr<nsIDocument> document = win->GetDoc();
  1.3289 +    JS::Rooted<JS::Value> v(cx);
  1.3290 +    nsresult rv = WrapNative(cx, document, document,
  1.3291 +                             &NS_GET_IID(nsIDOMDocument), &v, false);
  1.3292 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3293 +
  1.3294 +    // nsIDocument::WrapObject will handle defining the property.
  1.3295 +    *objp = obj;
  1.3296 +
  1.3297 +    // NB: We need to do this for any Xray wrapper.
  1.3298 +    if (xpc::WrapperFactory::IsXrayWrapper(obj)) {
  1.3299 +      *_retval = JS_WrapValue(cx, &v) &&
  1.3300 +                 JS_DefineProperty(cx, obj, "document", v,
  1.3301 +                                   JSPROP_READONLY | JSPROP_ENUMERATE,
  1.3302 +                                   JS_PropertyStub, JS_StrictPropertyStub);
  1.3303 +      if (!*_retval) {
  1.3304 +        return NS_ERROR_UNEXPECTED;
  1.3305 +      }
  1.3306 +    }
  1.3307 +
  1.3308 +    return NS_OK;
  1.3309 +  }
  1.3310 +
  1.3311 +  return nsDOMGenericSH::NewResolve(wrapper, cx, obj, id, objp, _retval);
  1.3312 +}
  1.3313 +
  1.3314 +NS_IMETHODIMP
  1.3315 +nsWindowSH::OuterObject(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
  1.3316 +                        JSObject * obj, JSObject * *_retval)
  1.3317 +{
  1.3318 +  nsGlobalWindow *origWin = nsGlobalWindow::FromWrapper(wrapper);
  1.3319 +  nsGlobalWindow *win = origWin->GetOuterWindowInternal();
  1.3320 +
  1.3321 +  if (!win) {
  1.3322 +    // If we no longer have an outer window. No code should ever be
  1.3323 +    // running on a window w/o an outer, which means this hook should
  1.3324 +    // never be called when we have no outer. But just in case, return
  1.3325 +    // null to prevent leaking an inner window to code in a different
  1.3326 +    // window.
  1.3327 +    *_retval = nullptr;
  1.3328 +    return NS_ERROR_UNEXPECTED;
  1.3329 +  }
  1.3330 +
  1.3331 +  JS::Rooted<JSObject*> winObj(cx, win->FastGetGlobalJSObject());
  1.3332 +  MOZ_ASSERT(winObj);
  1.3333 +
  1.3334 +  // Note that while |wrapper| is same-compartment with cx, the outer window
  1.3335 +  // might not be. If we're running script in an inactive scope and evalute
  1.3336 +  // |this|, the outer window is actually a cross-compartment wrapper. So we
  1.3337 +  // need to wrap here.
  1.3338 +  if (!JS_WrapObject(cx, &winObj)) {
  1.3339 +    *_retval = nullptr;
  1.3340 +    return NS_ERROR_UNEXPECTED;
  1.3341 +  }
  1.3342 +
  1.3343 +  *_retval = winObj;
  1.3344 +  return NS_OK;
  1.3345 +}
  1.3346 +
  1.3347 +NS_IMETHODIMP
  1.3348 +nsLocationSH::PreCreate(nsISupports *nativeObj, JSContext *cx,
  1.3349 +                        JSObject *globalObj, JSObject **parentObj)
  1.3350 +{
  1.3351 +  // window.location can be held onto by both evil pages that want to track the
  1.3352 +  // user's progress on the web and bookmarklets that want to use the location
  1.3353 +  // object. Parent it to the outer window so that access checks do the Right
  1.3354 +  // Thing.
  1.3355 +  *parentObj = globalObj;
  1.3356 +
  1.3357 +  nsCOMPtr<nsIDOMLocation> safeLoc(do_QueryInterface(nativeObj));
  1.3358 +  if (!safeLoc) {
  1.3359 +    // Oops, this wasn't really a location object. This can happen if someone
  1.3360 +    // tries to use our scriptable helper as a real object and tries to wrap
  1.3361 +    // it, see bug 319296
  1.3362 +    return NS_OK;
  1.3363 +  }
  1.3364 +
  1.3365 +  nsLocation *loc = (nsLocation *)safeLoc.get();
  1.3366 +  nsIDocShell *ds = loc->GetDocShell();
  1.3367 +  if (!ds) {
  1.3368 +    NS_WARNING("Refusing to create a location in the wrong scope");
  1.3369 +    return NS_ERROR_UNEXPECTED;
  1.3370 +  }
  1.3371 +
  1.3372 +  nsCOMPtr<nsIScriptGlobalObject> sgo = do_GetInterface(ds);
  1.3373 +  if (!sgo) {
  1.3374 +    NS_WARNING("Refusing to create a location in the wrong scope because the "
  1.3375 +               "docshell is being destroyed");
  1.3376 +    return NS_ERROR_UNEXPECTED;
  1.3377 +  }
  1.3378 +
  1.3379 +  *parentObj = sgo->GetGlobalJSObject();
  1.3380 +  return *parentObj ? NS_OK : NS_ERROR_FAILURE;
  1.3381 +}
  1.3382 +
  1.3383 +NS_IMETHODIMP
  1.3384 +nsLocationSH::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3385 +                          JSObject *obj, jsid aId, jsval *vp, bool *_retval)
  1.3386 +{
  1.3387 +  JS::Rooted<JSObject*> rootedObj(cx, obj);
  1.3388 +
  1.3389 +  // Shadowing protection. This will go away when nsLocation moves to the new
  1.3390 +  // bindings.
  1.3391 +  JS::Rooted<jsid> id(cx, aId);
  1.3392 +  if (wrapper->HasNativeMember(id)) {
  1.3393 +    JS_ReportError(cx, "Permission denied to shadow native property");
  1.3394 +    return NS_ERROR_FAILURE;
  1.3395 +  }
  1.3396 +
  1.3397 +  nsLocation* location = static_cast<nsLocation*>(GetNative(wrapper, rootedObj));
  1.3398 +  location->PreserveWrapper(location);
  1.3399 +
  1.3400 +  return NS_OK;
  1.3401 +}
  1.3402 +
  1.3403 +// EventTarget helper
  1.3404 +
  1.3405 +NS_IMETHODIMP
  1.3406 +nsEventTargetSH::PreCreate(nsISupports *nativeObj, JSContext *cx,
  1.3407 +                           JSObject *aGlobalObj, JSObject **parentObj)
  1.3408 +{
  1.3409 +  JS::Rooted<JSObject*> globalObj(cx, aGlobalObj);
  1.3410 +  DOMEventTargetHelper* target = DOMEventTargetHelper::FromSupports(nativeObj);
  1.3411 +
  1.3412 +  nsCOMPtr<nsIScriptGlobalObject> native_parent;
  1.3413 +  target->GetParentObject(getter_AddRefs(native_parent));
  1.3414 +
  1.3415 +  *parentObj = native_parent ? native_parent->GetGlobalJSObject() : globalObj;
  1.3416 +
  1.3417 +  return *parentObj ? NS_OK : NS_ERROR_FAILURE;
  1.3418 +}
  1.3419 +
  1.3420 +NS_IMETHODIMP
  1.3421 +nsEventTargetSH::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3422 +                             JSObject *obj, jsid id, jsval *vp, bool *_retval)
  1.3423 +{
  1.3424 +  nsEventTargetSH::PreserveWrapper(GetNative(wrapper, obj));
  1.3425 +
  1.3426 +  return NS_OK;
  1.3427 +}
  1.3428 +
  1.3429 +void
  1.3430 +nsEventTargetSH::PreserveWrapper(nsISupports *aNative)
  1.3431 +{
  1.3432 +  DOMEventTargetHelper* target = DOMEventTargetHelper::FromSupports(aNative);
  1.3433 +  target->PreserveWrapper(aNative);
  1.3434 +}
  1.3435 +
  1.3436 +// Generic array scriptable helper.
  1.3437 +
  1.3438 +NS_IMETHODIMP
  1.3439 +nsGenericArraySH::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3440 +                             JSObject *aObj, jsid aId, JSObject **objp,
  1.3441 +                             bool *_retval)
  1.3442 +{
  1.3443 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3444 +  JS::Rooted<jsid> id(cx, aId);
  1.3445 +  if (id == sLength_id) {
  1.3446 +    // Bail early; this isn't something we're interested in
  1.3447 +    return NS_OK;
  1.3448 +  }
  1.3449 +
  1.3450 +  bool is_number = false;
  1.3451 +  int32_t n = GetArrayIndexFromId(cx, id, &is_number);
  1.3452 +
  1.3453 +  if (is_number && n >= 0) {
  1.3454 +    // XXX The following is a cheap optimization to avoid hitting xpconnect to
  1.3455 +    // get the length. We may want to consider asking our concrete
  1.3456 +    // implementation for the length, and falling back onto the GetProperty if
  1.3457 +    // it doesn't provide one.
  1.3458 +
  1.3459 +    uint32_t length;
  1.3460 +    nsresult rv = GetLength(wrapper, cx, obj, &length);
  1.3461 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3462 +
  1.3463 +    uint32_t index = uint32_t(n);
  1.3464 +    if (index < length) {
  1.3465 +      *_retval = ::JS_DefineElement(cx, obj, index, JSVAL_VOID, nullptr, nullptr,
  1.3466 +                                    JSPROP_ENUMERATE | JSPROP_SHARED);
  1.3467 +      *objp = obj;
  1.3468 +    }
  1.3469 +  }
  1.3470 +
  1.3471 +  return NS_OK;
  1.3472 +}
  1.3473 +
  1.3474 +nsresult
  1.3475 +nsGenericArraySH::GetLength(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3476 +                            JS::Handle<JSObject*> obj, uint32_t *length)
  1.3477 +{
  1.3478 +  *length = 0;
  1.3479 +
  1.3480 +  JS::Rooted<JS::Value> lenval(cx);
  1.3481 +  if (!JS_GetProperty(cx, obj, "length", &lenval)) {
  1.3482 +    return NS_ERROR_UNEXPECTED;
  1.3483 +  }
  1.3484 +
  1.3485 +  if (!JSVAL_IS_INT(lenval)) {
  1.3486 +    // This can apparently happen with some sparse array impls falling back
  1.3487 +    // onto this code.
  1.3488 +    return NS_OK;
  1.3489 +  }
  1.3490 +
  1.3491 +  int32_t slen = JSVAL_TO_INT(lenval);
  1.3492 +  if (slen < 0) {
  1.3493 +    return NS_OK;
  1.3494 +  }
  1.3495 +
  1.3496 +  *length = (uint32_t)slen;
  1.3497 +
  1.3498 +  return NS_OK;
  1.3499 +}
  1.3500 +
  1.3501 +NS_IMETHODIMP
  1.3502 +nsGenericArraySH::Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3503 +                            JSObject *aObj, bool *_retval)
  1.3504 +{
  1.3505 +  // Recursion protection in case someone tries to be smart and call
  1.3506 +  // the enumerate hook from a user defined .length getter, or
  1.3507 +  // somesuch.
  1.3508 +
  1.3509 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3510 +  static bool sCurrentlyEnumerating;
  1.3511 +
  1.3512 +  if (sCurrentlyEnumerating) {
  1.3513 +    // Don't recurse to death.
  1.3514 +    return NS_OK;
  1.3515 +  }
  1.3516 +
  1.3517 +  sCurrentlyEnumerating = true;
  1.3518 +
  1.3519 +  JS::Rooted<JS::Value> len_val(cx);
  1.3520 +  bool ok = ::JS_GetProperty(cx, obj, "length", &len_val);
  1.3521 +
  1.3522 +  if (ok && JSVAL_IS_INT(len_val)) {
  1.3523 +    int32_t length = JSVAL_TO_INT(len_val);
  1.3524 +
  1.3525 +    for (int32_t i = 0; ok && i < length; ++i) {
  1.3526 +      ok = ::JS_DefineElement(cx, obj, i, JSVAL_VOID, nullptr, nullptr,
  1.3527 +                              JSPROP_ENUMERATE | JSPROP_SHARED);
  1.3528 +    }
  1.3529 +  }
  1.3530 +
  1.3531 +  sCurrentlyEnumerating = false;
  1.3532 +
  1.3533 +  return ok ? NS_OK : NS_ERROR_UNEXPECTED;
  1.3534 +}
  1.3535 +
  1.3536 +// Array scriptable helper
  1.3537 +
  1.3538 +NS_IMETHODIMP
  1.3539 +nsArraySH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3540 +                       JSObject *aObj, jsid aId, jsval *vp, bool *_retval)
  1.3541 +{
  1.3542 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3543 +  JS::Rooted<jsid> id(cx, aId);
  1.3544 +  bool is_number = false;
  1.3545 +  int32_t n = GetArrayIndexFromId(cx, id, &is_number);
  1.3546 +
  1.3547 +  nsresult rv = NS_OK;
  1.3548 +
  1.3549 +  if (is_number) {
  1.3550 +    if (n < 0) {
  1.3551 +      return NS_ERROR_DOM_INDEX_SIZE_ERR;
  1.3552 +    }
  1.3553 +
  1.3554 +    // Make sure rv == NS_OK here, so GetItemAt implementations that never fail
  1.3555 +    // don't have to set rv.
  1.3556 +    rv = NS_OK;
  1.3557 +    nsWrapperCache *cache = nullptr;
  1.3558 +    nsISupports* array_item =
  1.3559 +      GetItemAt(GetNative(wrapper, obj), n, &cache, &rv);
  1.3560 +    NS_ENSURE_SUCCESS(rv, rv);
  1.3561 +
  1.3562 +    if (array_item) {
  1.3563 +      JS::Rooted<JS::Value> rval(cx);
  1.3564 +      rv = WrapNative(cx, array_item, cache, true, &rval);
  1.3565 +      NS_ENSURE_SUCCESS(rv, rv);
  1.3566 +      *vp = rval;
  1.3567 +
  1.3568 +      rv = NS_SUCCESS_I_DID_SOMETHING;
  1.3569 +    }
  1.3570 +  }
  1.3571 +
  1.3572 +  return rv;
  1.3573 +}
  1.3574 +
  1.3575 +
  1.3576 +// CSSRuleList scriptable helper
  1.3577 +
  1.3578 +nsISupports*
  1.3579 +nsCSSRuleListSH::GetItemAt(nsISupports *aNative, uint32_t aIndex,
  1.3580 +                           nsWrapperCache **aCache, nsresult *aResult)
  1.3581 +{
  1.3582 +  nsICSSRuleList* list = static_cast<nsICSSRuleList*>(aNative);
  1.3583 +#ifdef DEBUG
  1.3584 +  {
  1.3585 +    nsCOMPtr<nsICSSRuleList> list_qi = do_QueryInterface(aNative);
  1.3586 +
  1.3587 +    // If this assertion fires the QI implementation for the object in
  1.3588 +    // question doesn't use the nsICSSRuleList pointer as the nsISupports
  1.3589 +    // pointer. That must be fixed, or we'll crash...
  1.3590 +    NS_ABORT_IF_FALSE(list_qi == list, "Uh, fix QI!");
  1.3591 +  }
  1.3592 +#endif
  1.3593 +
  1.3594 +  return list->Item(aIndex);
  1.3595 +}
  1.3596 +
  1.3597 +
  1.3598 +// Storage2SH
  1.3599 +
  1.3600 +// One reason we need a newResolve hook is that in order for
  1.3601 +// enumeration of storage object keys to work the keys we're
  1.3602 +// enumerating need to exist on the storage object for the JS engine
  1.3603 +// to find them.
  1.3604 +
  1.3605 +NS_IMETHODIMP
  1.3606 +nsStorage2SH::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3607 +                         JSObject *obj, jsid aId, JSObject **objp,
  1.3608 +                         bool *_retval)
  1.3609 +{
  1.3610 +  JS::Rooted<jsid> id(cx, aId);
  1.3611 +  if (ObjectIsNativeWrapper(cx, obj)) {
  1.3612 +    return NS_OK;
  1.3613 +  }
  1.3614 +
  1.3615 +  JS::Rooted<JSObject*> realObj(cx, wrapper->GetJSObject());
  1.3616 +
  1.3617 +  JSAutoCompartment ac(cx, realObj);
  1.3618 +
  1.3619 +  // First check to see if the property is defined on our prototype,
  1.3620 +  // after converting id to a string if it's an integer.
  1.3621 +
  1.3622 +  JS::Rooted<JSString*> jsstr(cx, IdToString(cx, id));
  1.3623 +  if (!jsstr) {
  1.3624 +    return NS_OK;
  1.3625 +  }
  1.3626 +
  1.3627 +  JS::Rooted<JSObject*> proto(cx);
  1.3628 +  if (!::JS_GetPrototype(cx, realObj, &proto)) {
  1.3629 +    return NS_ERROR_FAILURE;
  1.3630 +  }
  1.3631 +  bool hasProp;
  1.3632 +
  1.3633 +  if (proto &&
  1.3634 +      (::JS_HasPropertyById(cx, proto, id, &hasProp) &&
  1.3635 +       hasProp)) {
  1.3636 +    // We found the property we're resolving on the prototype,
  1.3637 +    // nothing left to do here then.
  1.3638 +
  1.3639 +    return NS_OK;
  1.3640 +  }
  1.3641 +
  1.3642 +  // We're resolving property that doesn't exist on the prototype,
  1.3643 +  // check if the key exists in the storage object.
  1.3644 +
  1.3645 +  nsCOMPtr<nsIDOMStorage> storage(do_QueryWrappedNative(wrapper));
  1.3646 +
  1.3647 +  nsDependentJSString depStr;
  1.3648 +  NS_ENSURE_TRUE(depStr.init(cx, jsstr), NS_ERROR_UNEXPECTED);
  1.3649 +
  1.3650 +  // GetItem() will return null if the caller can't access the session
  1.3651 +  // storage item.
  1.3652 +  nsAutoString data;
  1.3653 +  nsresult rv = storage->GetItem(depStr, data);
  1.3654 +  NS_ENSURE_SUCCESS(rv, rv);
  1.3655 +
  1.3656 +  if (!DOMStringIsNull(data)) {
  1.3657 +    if (!::JS_DefinePropertyById(cx, realObj, id, JSVAL_VOID, nullptr,
  1.3658 +                                 nullptr, JSPROP_ENUMERATE)) {
  1.3659 +      return NS_ERROR_FAILURE;
  1.3660 +    }
  1.3661 +
  1.3662 +    *objp = realObj;
  1.3663 +  }
  1.3664 +
  1.3665 +  return NS_OK;
  1.3666 +}
  1.3667 +
  1.3668 +NS_IMETHODIMP
  1.3669 +nsStorage2SH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3670 +                          JSObject *aObj, jsid aId, jsval *vp, bool *_retval)
  1.3671 +{
  1.3672 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3673 +  JS::Rooted<jsid> id(cx, aId);
  1.3674 +  nsCOMPtr<nsIDOMStorage> storage(do_QueryWrappedNative(wrapper));
  1.3675 +  NS_ENSURE_TRUE(storage, NS_ERROR_UNEXPECTED);
  1.3676 +
  1.3677 +  JSString* key = IdToString(cx, id);
  1.3678 +  NS_ENSURE_TRUE(key, NS_ERROR_UNEXPECTED);
  1.3679 +
  1.3680 +  nsDependentJSString keyStr;
  1.3681 +  NS_ENSURE_TRUE(keyStr.init(cx, key), NS_ERROR_UNEXPECTED);
  1.3682 +
  1.3683 +  // For native wrappers, do not get random names on storage objects.
  1.3684 +  if (ObjectIsNativeWrapper(cx, obj)) {
  1.3685 +    return NS_ERROR_NOT_AVAILABLE;
  1.3686 +  }
  1.3687 +
  1.3688 +  nsAutoString val;
  1.3689 +  nsresult rv = storage->GetItem(keyStr, val);
  1.3690 +  NS_ENSURE_SUCCESS(rv, rv);
  1.3691 +
  1.3692 +  if (DOMStringIsNull(val)) {
  1.3693 +    // No such key.
  1.3694 +    *vp = JSVAL_VOID;
  1.3695 +  } else {
  1.3696 +    JSString* str =
  1.3697 +      JS_NewUCStringCopyN(cx, static_cast<const jschar *>(val.get()),
  1.3698 +                          val.Length());
  1.3699 +    NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY);
  1.3700 +
  1.3701 +    *vp = STRING_TO_JSVAL(str);
  1.3702 +  }
  1.3703 +
  1.3704 +  return NS_SUCCESS_I_DID_SOMETHING;
  1.3705 +}
  1.3706 +
  1.3707 +NS_IMETHODIMP
  1.3708 +nsStorage2SH::SetProperty(nsIXPConnectWrappedNative *wrapper,
  1.3709 +                          JSContext *cx, JSObject *obj, jsid aId,
  1.3710 +                          jsval *vp, bool *_retval)
  1.3711 +{
  1.3712 +  JS::Rooted<jsid> id(cx, aId);
  1.3713 +  nsCOMPtr<nsIDOMStorage> storage(do_QueryWrappedNative(wrapper));
  1.3714 +  NS_ENSURE_TRUE(storage, NS_ERROR_UNEXPECTED);
  1.3715 +
  1.3716 +  JSString *key = IdToString(cx, id);
  1.3717 +  NS_ENSURE_TRUE(key, NS_ERROR_UNEXPECTED);
  1.3718 +
  1.3719 +  nsDependentJSString keyStr;
  1.3720 +  NS_ENSURE_TRUE(keyStr.init(cx, key), NS_ERROR_UNEXPECTED);
  1.3721 +
  1.3722 +  JS::Rooted<JS::Value> val(cx, *vp);
  1.3723 +  JSString *value = JS::ToString(cx, val);
  1.3724 +  NS_ENSURE_TRUE(value, NS_ERROR_UNEXPECTED);
  1.3725 +
  1.3726 +  nsDependentJSString valueStr;
  1.3727 +  NS_ENSURE_TRUE(valueStr.init(cx, value), NS_ERROR_UNEXPECTED);
  1.3728 +
  1.3729 +  nsresult rv = storage->SetItem(keyStr, valueStr);
  1.3730 +  if (NS_SUCCEEDED(rv)) {
  1.3731 +    rv = NS_SUCCESS_I_DID_SOMETHING;
  1.3732 +  }
  1.3733 +
  1.3734 +  return rv;
  1.3735 +}
  1.3736 +
  1.3737 +NS_IMETHODIMP
  1.3738 +nsStorage2SH::DelProperty(nsIXPConnectWrappedNative *wrapper,
  1.3739 +                          JSContext *cx, JSObject *obj, jsid aId,
  1.3740 +                          bool *_retval)
  1.3741 +{
  1.3742 +  JS::Rooted<jsid> id(cx, aId);
  1.3743 +  nsCOMPtr<nsIDOMStorage> storage(do_QueryWrappedNative(wrapper));
  1.3744 +  NS_ENSURE_TRUE(storage, NS_ERROR_UNEXPECTED);
  1.3745 +
  1.3746 +  JSString *key = IdToString(cx, id);
  1.3747 +  NS_ENSURE_TRUE(key, NS_ERROR_UNEXPECTED);
  1.3748 +
  1.3749 +  nsDependentJSString keyStr;
  1.3750 +  NS_ENSURE_TRUE(keyStr.init(cx, key), NS_ERROR_UNEXPECTED);
  1.3751 +
  1.3752 +  nsresult rv = storage->RemoveItem(keyStr);
  1.3753 +  if (NS_FAILED(rv)) {
  1.3754 +    return rv;
  1.3755 +  }
  1.3756 +
  1.3757 +  *_retval = true;
  1.3758 +  return NS_SUCCESS_I_DID_SOMETHING;
  1.3759 +}
  1.3760 +
  1.3761 +
  1.3762 +NS_IMETHODIMP
  1.3763 +nsStorage2SH::NewEnumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3764 +                           JSObject *obj, uint32_t enum_op, jsval *statep,
  1.3765 +                           jsid *idp, bool *_retval)
  1.3766 +{
  1.3767 +  if (enum_op == JSENUMERATE_INIT || enum_op == JSENUMERATE_INIT_ALL) {
  1.3768 +    nsCOMPtr<nsPIDOMStorage> storage(do_QueryWrappedNative(wrapper));
  1.3769 +
  1.3770 +    // XXXndeakin need to free the keys afterwards
  1.3771 +    nsTArray<nsString> *keys = storage->GetKeys();
  1.3772 +    NS_ENSURE_TRUE(keys, NS_ERROR_OUT_OF_MEMORY);
  1.3773 +
  1.3774 +    *statep = PRIVATE_TO_JSVAL(keys);
  1.3775 +
  1.3776 +    if (idp) {
  1.3777 +      *idp = INT_TO_JSID(keys->Length());
  1.3778 +    }
  1.3779 +    return NS_OK;
  1.3780 +  }
  1.3781 +
  1.3782 +  nsTArray<nsString> *keys =
  1.3783 +    (nsTArray<nsString> *)JSVAL_TO_PRIVATE(*statep);
  1.3784 +
  1.3785 +  if (enum_op == JSENUMERATE_NEXT && keys->Length() != 0) {
  1.3786 +    nsString& key = keys->ElementAt(0);
  1.3787 +    JS::Rooted<JSString*> str(cx, JS_NewUCStringCopyN(cx, key.get(), key.Length()));
  1.3788 +    NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY);
  1.3789 +
  1.3790 +    JS::Rooted<jsid> id(cx);
  1.3791 +    JS_StringToId(cx, str, &id);
  1.3792 +    *idp = id;
  1.3793 +
  1.3794 +    keys->RemoveElementAt(0);
  1.3795 +
  1.3796 +    return NS_OK;
  1.3797 +  }
  1.3798 +
  1.3799 +  // destroy the keys array if we have no keys or if we're done
  1.3800 +  NS_ABORT_IF_FALSE(enum_op == JSENUMERATE_DESTROY ||
  1.3801 +                    (enum_op == JSENUMERATE_NEXT && keys->Length() == 0),
  1.3802 +                    "Bad call from the JS engine");
  1.3803 +  delete keys;
  1.3804 +
  1.3805 +  *statep = JSVAL_NULL;
  1.3806 +
  1.3807 +  return NS_OK;
  1.3808 +}
  1.3809 +
  1.3810 +// nsIDOMEventListener::HandleEvent() 'this' converter helper
  1.3811 +
  1.3812 +NS_INTERFACE_MAP_BEGIN(nsEventListenerThisTranslator)
  1.3813 +  NS_INTERFACE_MAP_ENTRY(nsIXPCFunctionThisTranslator)
  1.3814 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
  1.3815 +NS_INTERFACE_MAP_END
  1.3816 +
  1.3817 +
  1.3818 +NS_IMPL_ADDREF(nsEventListenerThisTranslator)
  1.3819 +NS_IMPL_RELEASE(nsEventListenerThisTranslator)
  1.3820 +
  1.3821 +
  1.3822 +NS_IMETHODIMP
  1.3823 +nsEventListenerThisTranslator::TranslateThis(nsISupports *aInitialThis,
  1.3824 +                                             nsISupports **_retval)
  1.3825 +{
  1.3826 +  nsCOMPtr<nsIDOMEvent> event(do_QueryInterface(aInitialThis));
  1.3827 +  NS_ENSURE_TRUE(event, NS_ERROR_UNEXPECTED);
  1.3828 +
  1.3829 +  nsCOMPtr<EventTarget> target = event->InternalDOMEvent()->GetCurrentTarget();
  1.3830 +  target.forget(_retval);
  1.3831 +  return NS_OK;
  1.3832 +}
  1.3833 +
  1.3834 +NS_IMETHODIMP
  1.3835 +nsDOMConstructorSH::PreCreate(nsISupports *nativeObj, JSContext *cx,
  1.3836 +                              JSObject *aGlobalObj, JSObject **parentObj)
  1.3837 +{
  1.3838 +  JS::Rooted<JSObject*> globalObj(cx, aGlobalObj);
  1.3839 +  nsDOMConstructor *wrapped = static_cast<nsDOMConstructor *>(nativeObj);
  1.3840 +
  1.3841 +#ifdef DEBUG
  1.3842 +  {
  1.3843 +    nsCOMPtr<nsIDOMDOMConstructor> is_constructor =
  1.3844 +      do_QueryInterface(nativeObj);
  1.3845 +    NS_ASSERTION(is_constructor, "How did we not get a constructor?");
  1.3846 +  }
  1.3847 +#endif
  1.3848 +
  1.3849 +  return wrapped->PreCreate(cx, globalObj, parentObj);
  1.3850 +}
  1.3851 +
  1.3852 +NS_IMETHODIMP
  1.3853 +nsDOMConstructorSH::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3854 +                               JSObject *aObj, jsid aId, JSObject **objp,
  1.3855 +                               bool *_retval)
  1.3856 +{
  1.3857 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3858 +  JS::Rooted<jsid> id(cx, aId);
  1.3859 +  // For regular DOM constructors, we have our interface constants defined on
  1.3860 +  // us by nsWindowSH::GlobalResolve. However, XrayWrappers can't see these
  1.3861 +  // interface constants (as they look like expando properties) so we have to
  1.3862 +  // specially resolve those constants here, but only for Xray wrappers.
  1.3863 +  if (!ObjectIsNativeWrapper(cx, obj)) {
  1.3864 +    return NS_OK;
  1.3865 +  }
  1.3866 +
  1.3867 +  JS::Rooted<JSObject*> nativePropsObj(cx, xpc::XrayUtils::GetNativePropertiesObject(cx, obj));
  1.3868 +  nsDOMConstructor *wrapped =
  1.3869 +    static_cast<nsDOMConstructor *>(wrapper->Native());
  1.3870 +  nsresult rv = wrapped->ResolveInterfaceConstants(cx, nativePropsObj);
  1.3871 +  NS_ENSURE_SUCCESS(rv, rv);
  1.3872 +
  1.3873 +  // Now re-lookup the ID to see if we should report back that we resolved the
  1.3874 +  // looked-for constant. Note that we don't have to worry about infinitely
  1.3875 +  // recurring back here because the Xray wrapper's holder object doesn't call
  1.3876 +  // NewResolve hooks.
  1.3877 +  bool found;
  1.3878 +  if (!JS_HasPropertyById(cx, nativePropsObj, id, &found)) {
  1.3879 +    *_retval = false;
  1.3880 +    return NS_OK;
  1.3881 +  }
  1.3882 +
  1.3883 +  if (found) {
  1.3884 +    *objp = obj;
  1.3885 +  }
  1.3886 +  return NS_OK;
  1.3887 +}
  1.3888 +
  1.3889 +NS_IMETHODIMP
  1.3890 +nsDOMConstructorSH::Call(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3891 +                         JSObject *aObj, const JS::CallArgs &args, bool *_retval)
  1.3892 +{
  1.3893 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3894 +  MOZ_ASSERT(obj);
  1.3895 +
  1.3896 +  nsDOMConstructor *wrapped =
  1.3897 +    static_cast<nsDOMConstructor *>(wrapper->Native());
  1.3898 +
  1.3899 +#ifdef DEBUG
  1.3900 +  {
  1.3901 +    nsCOMPtr<nsIDOMDOMConstructor> is_constructor =
  1.3902 +      do_QueryWrappedNative(wrapper);
  1.3903 +    NS_ASSERTION(is_constructor, "How did we not get a constructor?");
  1.3904 +  }
  1.3905 +#endif
  1.3906 +
  1.3907 +  return wrapped->Construct(wrapper, cx, obj, args, _retval);
  1.3908 +}
  1.3909 +
  1.3910 +NS_IMETHODIMP
  1.3911 +nsDOMConstructorSH::Construct(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
  1.3912 +                              JSObject *aObj, const JS::CallArgs &args, bool *_retval)
  1.3913 +{
  1.3914 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3915 +  MOZ_ASSERT(obj);
  1.3916 +
  1.3917 +  nsDOMConstructor *wrapped =
  1.3918 +    static_cast<nsDOMConstructor *>(wrapper->Native());
  1.3919 +
  1.3920 +#ifdef DEBUG
  1.3921 +  {
  1.3922 +    nsCOMPtr<nsIDOMDOMConstructor> is_constructor =
  1.3923 +      do_QueryWrappedNative(wrapper);
  1.3924 +    NS_ASSERTION(is_constructor, "How did we not get a constructor?");
  1.3925 +  }
  1.3926 +#endif
  1.3927 +
  1.3928 +  return wrapped->Construct(wrapper, cx, obj, args, _retval);
  1.3929 +}
  1.3930 +
  1.3931 +NS_IMETHODIMP
  1.3932 +nsDOMConstructorSH::HasInstance(nsIXPConnectWrappedNative *wrapper,
  1.3933 +                                JSContext *cx, JSObject *aObj, JS::Handle<JS::Value> val,
  1.3934 +                                bool *bp, bool *_retval)
  1.3935 +{
  1.3936 +  JS::Rooted<JSObject*> obj(cx, aObj);
  1.3937 +  nsDOMConstructor *wrapped =
  1.3938 +    static_cast<nsDOMConstructor *>(wrapper->Native());
  1.3939 +
  1.3940 +#ifdef DEBUG
  1.3941 +  {
  1.3942 +    nsCOMPtr<nsIDOMDOMConstructor> is_constructor =
  1.3943 +      do_QueryWrappedNative(wrapper);
  1.3944 +    NS_ASSERTION(is_constructor, "How did we not get a constructor?");
  1.3945 +  }
  1.3946 +#endif
  1.3947 +
  1.3948 +  return wrapped->HasInstance(wrapper, cx, obj, val, bp, _retval);
  1.3949 +}
  1.3950 +
  1.3951 +NS_IMETHODIMP
  1.3952 +nsNonDOMObjectSH::GetFlags(uint32_t *aFlags)
  1.3953 +{
  1.3954 +  // This is NOT a DOM Object.  Use this helper class for cases when you need
  1.3955 +  // to do something like implement nsISecurityCheckedComponent in a meaningful
  1.3956 +  // way.
  1.3957 +  *aFlags = nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::SINGLETON_CLASSINFO;
  1.3958 +  return NS_OK;
  1.3959 +}

mercurial