Bug 1477432 - Part 11: Update CustomElementRegistry to not use nsIJSID, r=smaug
authorNika Layzell <nika@thelayzells.com>
Tue, 23 Oct 2018 23:16:01 -0400
changeset 503342 241738774bb1b1f998770af0e0dd82c6ba8de36d
parent 503341 29c30b62c301eacd5fefd9a78f19cf63fafd889a
child 503343 896d672ed0abaece6317543d2d14783044c2709a
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1477432
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1477432 - Part 11: Update CustomElementRegistry to not use nsIJSID, r=smaug Differential Revision: https://phabricator.services.mozilla.com/D9733
dom/base/CustomElementRegistry.cpp
dom/webidl/WebComponents.webidl
--- a/dom/base/CustomElementRegistry.cpp
+++ b/dom/base/CustomElementRegistry.cpp
@@ -1248,47 +1248,61 @@ CustomElementRegistry::Upgrade(Element* 
 }
 
 already_AddRefed<nsISupports>
 CustomElementRegistry::CallGetCustomInterface(Element* aElement,
                                               const nsIID& aIID)
 {
   MOZ_ASSERT(aElement);
 
-  if (nsContentUtils::IsChromeDoc(aElement->OwnerDoc())) {
-    CustomElementDefinition* definition = aElement->GetCustomElementDefinition();
-    if (definition && definition->mCallbacks &&
-        definition->mCallbacks->mGetCustomInterfaceCallback.WasPassed() &&
-        definition->mLocalName == aElement->NodeInfo()->NameAtom()) {
-
-      LifecycleGetCustomInterfaceCallback* func =
-        definition->mCallbacks->mGetCustomInterfaceCallback.Value();
-      JS::Rooted<JSObject*> customInterface(RootingCx());
+  if (!nsContentUtils::IsChromeDoc(aElement->OwnerDoc())) {
+    return nullptr;
+  }
 
-      nsCOMPtr<nsIJSID> iid = nsJSID::NewID(aIID);
-      func->Call(aElement, iid, &customInterface);
-      JS::Rooted<JSObject*> funcGlobal(RootingCx(), func->CallbackGlobalOrNull());
-      if (customInterface && funcGlobal) {
-        AutoJSAPI jsapi;
-        if (jsapi.Init(funcGlobal)) {
-          nsIXPConnect *xpConnect = nsContentUtils::XPConnect();
-          JSContext* cx = jsapi.cx();
+  // Try to get our GetCustomInterfaceCallback callback.
+  CustomElementDefinition* definition = aElement->GetCustomElementDefinition();
+  if (!definition || !definition->mCallbacks ||
+      !definition->mCallbacks->mGetCustomInterfaceCallback.WasPassed() ||
+      (definition->mLocalName != aElement->NodeInfo()->NameAtom())) {
+    return nullptr;
+  }
+  LifecycleGetCustomInterfaceCallback* func =
+    definition->mCallbacks->mGetCustomInterfaceCallback.Value();
 
-          nsCOMPtr<nsISupports> wrapper;
-          nsresult rv = xpConnect->WrapJSAggregatedToNative(aElement, cx, customInterface,
-                                                            aIID, getter_AddRefs(wrapper));
-          if (NS_SUCCEEDED(rv)) {
-            return wrapper.forget();
-          }
-        }
-      }
-    }
+  // Initialize a AutoJSAPI to enter the compartment of the callback.
+  AutoJSAPI jsapi;
+  JS::RootedObject funcGlobal(RootingCx(), func->CallbackGlobalOrNull());
+  if (!funcGlobal || !jsapi.Init(funcGlobal)) {
+    return nullptr;
   }
 
-  return nullptr;
+  // Grab our JSContext.
+  JSContext* cx = jsapi.cx();
+
+  // Convert our IID to a JSValue to call our callback.
+  JS::RootedValue jsiid(cx);
+  if (!xpc::ID2JSValue(cx, aIID, &jsiid)) {
+    return nullptr;
+  }
+
+  JS::RootedObject customInterface(cx);
+  func->Call(aElement, jsiid, &customInterface);
+  if (!customInterface) {
+    return nullptr;
+  }
+
+  // Wrap our JSObject into a nsISupports through XPConnect
+  nsCOMPtr<nsISupports> wrapper;
+  nsresult rv = nsContentUtils::XPConnect()->WrapJSAggregatedToNative(
+    aElement, cx, customInterface, aIID, getter_AddRefs(wrapper));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return nullptr;
+  }
+
+  return wrapper.forget();
 }
 
 //-----------------------------------------------------
 // CustomElementReactionsStack
 
 void
 CustomElementReactionsStack::CreateAndPushElementQueue()
 {
--- a/dom/webidl/WebComponents.webidl
+++ b/dom/webidl/WebComponents.webidl
@@ -5,27 +5,25 @@
  *
  * The origin of this IDL file is
  * http://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/custom/index.html
  *
  * Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C
  * liability, trademark and document use rules apply.
  */
 
-interface IID;
-
 callback LifecycleConnectedCallback = void();
 callback LifecycleDisconnectedCallback = void();
 callback LifecycleAdoptedCallback = void(Document? oldDocument,
                                          Document? newDocment);
 callback LifecycleAttributeChangedCallback = void(DOMString attrName,
                                                   DOMString? oldValue,
                                                   DOMString? newValue,
                                                   DOMString? namespaceURI);
-callback LifecycleGetCustomInterfaceCallback = object?(IID iid);
+callback LifecycleGetCustomInterfaceCallback = object?(any iid);
 
 dictionary LifecycleCallbacks {
   LifecycleConnectedCallback connectedCallback;
   LifecycleDisconnectedCallback disconnectedCallback;
   LifecycleAdoptedCallback adoptedCallback;
   LifecycleAttributeChangedCallback attributeChangedCallback;
   [ChromeOnly] LifecycleGetCustomInterfaceCallback getCustomInterfaceCallback;
 };