Bug 842818 - Inline CloneData() and clean up ImportKeyTask::SetKeyData() r=rbarnes
authorTim Taubert <ttaubert@mozilla.com>
Thu, 17 Mar 2016 11:02:11 +0100
changeset 330299 17e4c84b299d
parent 330298 9262f09b3322
child 330300 4fe4eae85573
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrbarnes
bugs842818
milestone48.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 842818 - Inline CloneData() and clean up ImportKeyTask::SetKeyData() r=rbarnes
dom/crypto/WebCryptoTask.cpp
--- a/dom/crypto/WebCryptoTask.cpp
+++ b/dom/crypto/WebCryptoTask.cpp
@@ -309,37 +309,16 @@ MapHashAlgorithmNameToMgfMechanism(const
     mech = CKG_MGF1_SHA384;
   } else if (aName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) {
     mech = CKG_MGF1_SHA512;
   }
 
   return mech;
 }
 
-// Helper function to clone data from an ArrayBuffer or ArrayBufferView object
-inline bool
-CloneData(JSContext* aCx, CryptoBuffer& aDst, JS::Handle<JSObject*> aSrc)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-
-  // Try ArrayBuffer
-  RootedTypedArray<ArrayBuffer> ab(aCx);
-  if (ab.Init(aSrc)) {
-    return !!aDst.Assign(ab);
-  }
-
-  // Try ArrayBufferView
-  RootedTypedArray<ArrayBufferView> abv(aCx);
-  if (abv.Init(aSrc)) {
-    return !!aDst.Assign(abv);
-  }
-
-  return false;
-}
-
 // Implementation of WebCryptoTask methods
 
 void
 WebCryptoTask::DispatchWithPromise(Promise* aResultPromise)
 {
   mResultPromise = aResultPromise;
 
   // Fail if an error was set during the constructor
@@ -1374,66 +1353,90 @@ public:
         }
       }
     }
 
     // Individual algorithms may still have to check 'use'
     return true;
   }
 
-  void SetKeyData(JSContext* aCx, JS::Handle<JSObject*> aKeyData) {
-    // First try to treat as ArrayBuffer/ABV,
-    // and if that fails, try to initialize a JWK
-    if (CloneData(aCx, mKeyData, aKeyData)) {
-      mDataIsJwk = false;
-
-      if (mFormat.EqualsLiteral(WEBCRYPTO_KEY_FORMAT_JWK)) {
-        SetJwkFromKeyData();
+  void SetKeyData(JSContext* aCx, JS::Handle<JSObject*> aKeyData)
+  {
+    mDataIsJwk = false;
+
+    // Try ArrayBuffer
+    RootedTypedArray<ArrayBuffer> ab(aCx);
+    if (ab.Init(aKeyData)) {
+      if (!mKeyData.Assign(ab)) {
+        mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
       }
-    } else {
-      ClearException ce(aCx);
-      JS::RootedValue value(aCx, JS::ObjectValue(*aKeyData));
-      if (!mJwk.Init(aCx, value)) {
-        mEarlyRv = NS_ERROR_DOM_DATA_ERR;
-        return;
+      return;
+    }
+
+    // Try ArrayBufferView
+    RootedTypedArray<ArrayBufferView> abv(aCx);
+    if (abv.Init(aKeyData)) {
+      if (!mKeyData.Assign(abv)) {
+        mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
       }
-      mDataIsJwk = true;
+      return;
     }
+
+    // Try JWK
+    ClearException ce(aCx);
+    JS::RootedValue value(aCx, JS::ObjectValue(*aKeyData));
+    if (!mJwk.Init(aCx, value)) {
+      mEarlyRv = NS_ERROR_DOM_DATA_ERR;
+      return;
+    }
+
+    mDataIsJwk = true;
   }
 
-  void SetKeyData(const CryptoBuffer& aKeyData)
+  void SetKeyDataMaybeParseJWK(const CryptoBuffer& aKeyData)
   {
     if (!mKeyData.Assign(aKeyData)) {
       mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
       return;
     }
 
     mDataIsJwk = false;
 
     if (mFormat.EqualsLiteral(WEBCRYPTO_KEY_FORMAT_JWK)) {
-      SetJwkFromKeyData();
+      nsDependentCSubstring utf8((const char*) mKeyData.Elements(),
+                                 (const char*) (mKeyData.Elements() +
+                                                mKeyData.Length()));
+      if (!IsUTF8(utf8)) {
+        mEarlyRv = NS_ERROR_DOM_DATA_ERR;
+        return;
+      }
+
+      nsString json = NS_ConvertUTF8toUTF16(utf8);
+      if (!mJwk.Init(json)) {
+        mEarlyRv = NS_ERROR_DOM_DATA_ERR;
+        return;
+      }
+
+      mDataIsJwk = true;
     }
   }
 
-  void SetJwkFromKeyData()
+  void SetRawKeyData(const CryptoBuffer& aKeyData)
   {
-    nsDependentCSubstring utf8((const char*) mKeyData.Elements(),
-                               (const char*) (mKeyData.Elements() +
-                                              mKeyData.Length()));
-    if (!IsUTF8(utf8)) {
-      mEarlyRv = NS_ERROR_DOM_DATA_ERR;
+    if (!mFormat.EqualsLiteral(WEBCRYPTO_KEY_FORMAT_RAW)) {
+      mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
       return;
     }
 
-    nsString json = NS_ConvertUTF8toUTF16(utf8);
-    if (!mJwk.Init(json)) {
-      mEarlyRv = NS_ERROR_DOM_DATA_ERR;
+    if (!mKeyData.Assign(aKeyData)) {
+      mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
       return;
     }
-    mDataIsJwk = true;
+
+    mDataIsJwk = false;
   }
 
 protected:
   nsString mFormat;
   RefPtr<CryptoKey> mKey;
   CryptoBuffer mKeyData;
   bool mDataIsSet;
   bool mDataIsJwk;
@@ -2841,17 +2844,17 @@ public:
   }
 
 protected:
   RefPtr<ImportSymmetricKeyTask> mTask;
   bool mResolved;
 
 private:
   virtual void Resolve() override {
-    mTask->SetKeyData(this->mResult);
+    mTask->SetRawKeyData(this->mResult);
     mTask->DispatchWithPromise(this->mResultPromise);
     mResolved = true;
   }
 
   virtual void Cleanup() override
   {
     if (mTask && !mResolved) {
       mTask->Skip();
@@ -3136,17 +3139,17 @@ public:
   {}
 
 private:
   RefPtr<ImportKeyTask> mTask;
   bool mResolved;
 
   virtual void Resolve() override
   {
-    mTask->SetKeyData(KeyEncryptTask::mResult);
+    mTask->SetKeyDataMaybeParseJWK(KeyEncryptTask::mResult);
     mTask->DispatchWithPromise(KeyEncryptTask::mResultPromise);
     mResolved = true;
   }
 
   virtual void Cleanup() override
   {
     if (mTask && !mResolved) {
       mTask->Skip();