Bug 1258299 - Move RTCIdentityProvider to a dictionary, r=jib,khuey draft
authorMartin Thomson <martin.thomson@gmail.com>
Thu, 24 Mar 2016 03:03:06 +1100
changeset 344193 df44c64ca5614b1ff351d3aa9ffc78e513f97d58
parent 344192 b0f1eb9c1f386de175b3c2e06d85d9896c33353e
child 516904 6f13948a38555e20e64bbfed04f7f5803b0ece64
push id13770
push usermartin.thomson@gmail.com
push dateWed, 23 Mar 2016 23:45:46 +0000
reviewersjib, khuey
bugs1258299
milestone48.0a1
Bug 1258299 - Move RTCIdentityProvider to a dictionary, r=jib,khuey MozReview-Commit-ID: EP1fCvQlYqH
dom/media/IdpSandbox.jsm
dom/media/tests/mochitest/identity/idp.js
dom/media/webrtc/RTCIdentityProviderRegistrar.cpp
dom/media/webrtc/RTCIdentityProviderRegistrar.h
dom/webidl/RTCIdentityProvider.webidl
--- a/dom/media/IdpSandbox.jsm
+++ b/dom/media/IdpSandbox.jsm
@@ -226,17 +226,17 @@ IdpSandbox.prototype = {
       // as being IdP errors by the IdP and we drop line numbers as a result.
       if (e.name === 'IdpError' || e.name === 'IdpLoginError') {
         throw e;
       }
       this._logError(e);
       throw new Error('Error in IdP, check console for details');
     }
 
-    if (!registrar.idp) {
+    if (!registrar.hasIdp) {
       throw new Error('IdP failed to call rtcIdentityProvider.register()');
     }
     return registrar;
   },
 
   // Capture all the details from the error and log them to the console.  This
   // can't rethrow anything else because that could leak information about the
   // internal workings of the IdP across origins.
--- a/dom/media/tests/mochitest/identity/idp.js
+++ b/dom/media/tests/mochitest/identity/idp.js
@@ -96,11 +96,15 @@
           identity: assertion.username,
           contents: assertion.contents
         });
     }
   };
 
   if (!instructions.some(is('not_ready'))) {
     dump('registering idp.js' + global.location.hash + '\n');
-    global.rtcIdentityProvider.register(new IDPJS());
+    var idp = new IDPJS();
+    global.rtcIdentityProvider.register({
+      generateAssertion: idp.generateAssertion.bind(idp),
+      validateAssertion: idp.validateAssertion.bind(idp)
+    });
   }
 }(this));
--- a/dom/media/webrtc/RTCIdentityProviderRegistrar.cpp
+++ b/dom/media/webrtc/RTCIdentityProviderRegistrar.cpp
@@ -1,36 +1,38 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "RTCIdentityProviderRegistrar.h"
 #include "mozilla/Attributes.h"
-#include "mozilla/dom/RTCIdentityProviderBinding.h"
 #include "nsCycleCollectionParticipant.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RTCIdentityProviderRegistrar)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(RTCIdentityProviderRegistrar)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(RTCIdentityProviderRegistrar)
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(RTCIdentityProviderRegistrar,
-                                      mGlobal, mIdp)
+                                      mGlobal,
+                                      mGenerateAssertionCallback,
+                                      mValidateAssertionCallback)
 
 RTCIdentityProviderRegistrar::RTCIdentityProviderRegistrar(
     nsIGlobalObject* aGlobal)
     : mGlobal(aGlobal)
-    , mIdp(nullptr)
+    , mGenerateAssertionCallback(nullptr)
+    , mValidateAssertionCallback(nullptr)
 {
   MOZ_COUNT_CTOR(RTCIdentityProviderRegistrar);
 }
 
 RTCIdentityProviderRegistrar::~RTCIdentityProviderRegistrar()
 {
   MOZ_COUNT_DTOR(RTCIdentityProviderRegistrar);
 }
@@ -43,46 +45,46 @@ RTCIdentityProviderRegistrar::GetParentO
 
 JSObject*
 RTCIdentityProviderRegistrar::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return RTCIdentityProviderRegistrarBinding::Wrap(aCx, this, aGivenProto);
 }
 
 void
-RTCIdentityProviderRegistrar::Register(RTCIdentityProvider& aIdp)
+RTCIdentityProviderRegistrar::Register(const RTCIdentityProvider& aIdp)
 {
-  mIdp = &aIdp;
+  mGenerateAssertionCallback = aIdp.mGenerateAssertion;
+  mValidateAssertionCallback = aIdp.mValidateAssertion;
 }
 
-already_AddRefed<RTCIdentityProvider>
-RTCIdentityProviderRegistrar::GetIdp()
+bool
+RTCIdentityProviderRegistrar::HasIdp() const
 {
-  RefPtr<RTCIdentityProvider> idp = mIdp;
-  return idp.forget();
+  return mGenerateAssertionCallback && mValidateAssertionCallback;
 }
 
 already_AddRefed<Promise>
 RTCIdentityProviderRegistrar::GenerateAssertion(
   const nsAString& aContents, const nsAString& aOrigin,
   const Optional<nsAString>& aUsernameHint, ErrorResult& aRv)
 {
-  if (!mIdp) {
+  if (!mGenerateAssertionCallback) {
     aRv.Throw(NS_ERROR_NOT_INITIALIZED);
     return nullptr;
   }
-  return mIdp->GenerateAssertion(aContents, aOrigin, aUsernameHint, aRv);
+  return mGenerateAssertionCallback->Call(aContents, aOrigin, aUsernameHint, aRv);
 }
 already_AddRefed<Promise>
 RTCIdentityProviderRegistrar::ValidateAssertion(
   const nsAString& aAssertion, const nsAString& aOrigin, ErrorResult& aRv)
 {
-  if (!mIdp) {
+  if (!mValidateAssertionCallback) {
     aRv.Throw(NS_ERROR_NOT_INITIALIZED);
     return nullptr;
   }
-  return mIdp->ValidateAssertion(aAssertion, aOrigin, aRv);
+  return mValidateAssertionCallback->Call(aAssertion, aOrigin, aRv);
 }
 
 
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/webrtc/RTCIdentityProviderRegistrar.h
+++ b/dom/media/webrtc/RTCIdentityProviderRegistrar.h
@@ -9,49 +9,51 @@
 #include "mozilla/RefPtr.h"
 #include "nsCOMPtr.h"
 #include "nsISupportsImpl.h"
 #include "nsIGlobalObject.h"
 #include "nsWrapperCache.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/BindingDeclarations.h"
+#include "mozilla/dom/RTCIdentityProviderBinding.h"
 
 namespace mozilla {
 namespace dom {
 
-class RTCIdentityProvider;
+struct RTCIdentityProvider;
 
 class RTCIdentityProviderRegistrar final : public nsISupports,
                                            public nsWrapperCache
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(RTCIdentityProviderRegistrar)
 
   explicit RTCIdentityProviderRegistrar(nsIGlobalObject* aGlobal);
 
   // As required
   nsIGlobalObject* GetParentObject() const;
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
-  // setter and getter
-  void Register(RTCIdentityProvider& aIdp);
-  already_AddRefed<RTCIdentityProvider> GetIdp();
+  // setter and checker
+  void Register(const RTCIdentityProvider& aIdp);
+  bool HasIdp() const;
 
   already_AddRefed<Promise>
   GenerateAssertion(const nsAString& aContents, const nsAString& aOrigin,
                     const Optional<nsAString>& aUsernameHint, ErrorResult& aRv);
   already_AddRefed<Promise>
   ValidateAssertion(const nsAString& assertion, const nsAString& origin,
                     ErrorResult& aRv);
 
 private:
   ~RTCIdentityProviderRegistrar();
 
   nsCOMPtr<nsIGlobalObject> mGlobal;
-  RefPtr<RTCIdentityProvider> mIdp;
+  RefPtr<GenerateAssertionCallback> mGenerateAssertionCallback;
+  RefPtr<ValidateAssertionCallback> mValidateAssertionCallback;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif /* RTCIDENTITYPROVIDER_H_ */
--- a/dom/webidl/RTCIdentityProvider.webidl
+++ b/dom/webidl/RTCIdentityProvider.webidl
@@ -5,19 +5,19 @@
  *
  * http://w3c.github.io/webrtc-pc/ (with https://github.com/w3c/webrtc-pc/pull/178)
  */
 
 [NoInterfaceObject]
 interface RTCIdentityProviderRegistrar {
   void register(RTCIdentityProvider idp);
 
-  /* The IdP that was passed to register() to chrome code, if any. */
+  /* If an IdP was passed to register() to chrome code. */
   [ChromeOnly]
-  readonly attribute RTCIdentityProvider? idp;
+  readonly attribute boolean hasIdp;
   /* The following two chrome-only functions forward to the corresponding
    * function on the registered IdP.  This is necessary because the
    * JS-implemented WebIDL can't see these functions on `idp` above, chrome JS
    * gets an Xray onto the content code that suppresses functions, see
    * https://developer.mozilla.org/en-US/docs/Xray_vision#Xrays_for_JavaScript_objects
    */
   /* Forward to idp.generateAssertion() */
   [ChromeOnly, Throws]
@@ -25,23 +25,26 @@ interface RTCIdentityProviderRegistrar {
   generateAssertion(DOMString contents, DOMString origin,
                     optional DOMString usernameHint);
   /* Forward to idp.validateAssertion() */
   [ChromeOnly, Throws]
   Promise<RTCIdentityValidationResult>
   validateAssertion(DOMString assertion, DOMString origin);
 };
 
-callback interface RTCIdentityProvider {
+dictionary RTCIdentityProvider {
+  required GenerateAssertionCallback generateAssertion;
+  required ValidateAssertionCallback validateAssertion;
+};
+
+callback GenerateAssertionCallback =
   Promise<RTCIdentityAssertionResult>
-  generateAssertion(DOMString contents, DOMString origin,
-                    optional DOMString usernameHint);
-  Promise<RTCIdentityValidationResult>
-  validateAssertion(DOMString assertion, DOMString origin);
-};
+    (DOMString contents, DOMString origin, optional DOMString usernameHint);
+callback ValidateAssertionCallback =
+  Promise<RTCIdentityValidationResult> (DOMString assertion, DOMString origin);
 
 dictionary RTCIdentityAssertionResult {
   required RTCIdentityProviderDetails idp;
   required DOMString assertion;
 };
 
 dictionary RTCIdentityProviderDetails {
   required DOMString domain;