This commit is contained in:
Cameron Kaiser 2017-05-29 20:31:35 -07:00
parent 7bf1baf33f
commit b6849d2795
1 changed files with 24 additions and 11 deletions

View File

@ -75,7 +75,8 @@ public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIIDENTITYKEYPAIR
KeyPair(SECKEYPrivateKey* aPrivateKey, SECKEYPublicKey* aPublicKey);
KeyPair(SECKEYPrivateKey* aPrivateKey, SECKEYPublicKey* aPublicKey,
nsIEventTarget* aOperationThread);
private:
~KeyPair()
@ -103,6 +104,7 @@ private:
SECKEYPrivateKey * mPrivateKey;
SECKEYPublicKey * mPublicKey;
nsCOMPtr<nsIEventTarget> mThread;
KeyPair(const KeyPair &) = delete;
void operator=(const KeyPair &) = delete;
@ -115,7 +117,8 @@ class KeyGenRunnable : public nsRunnable, public nsNSSShutDownObject
public:
NS_DECL_NSIRUNNABLE
KeyGenRunnable(KeyType keyType, nsIIdentityKeyGenCallback * aCallback);
KeyGenRunnable(KeyType keyType, nsIIdentityKeyGenCallback * aCallback,
nsIEventTarget* aOperationThread);
private:
~KeyGenRunnable()
@ -141,6 +144,7 @@ private:
nsMainThreadPtrHandle<nsIIdentityKeyGenCallback> mCallback; // in
nsresult mRv; // out
nsCOMPtr<nsIIdentityKeyPair> mKeyPair; // out
nsCOMPtr<nsIEventTarget> mThread;
KeyGenRunnable(const KeyGenRunnable &) = delete;
void operator=(const KeyGenRunnable &) = delete;
@ -201,6 +205,12 @@ public:
= do_GetService("@mozilla.org/psm;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIThread> thread;
rv = NS_NewNamedThread("IdentityCrypto", getter_AddRefs(thread));
NS_ENSURE_SUCCESS(rv, rv);
mThread = thread.forget();
return NS_OK;
}
@ -208,6 +218,8 @@ private:
~IdentityCryptoService() { }
IdentityCryptoService(const KeyPair &) = delete;
void operator=(const IdentityCryptoService &) = delete;
nsCOMPtr<nsIEventTarget> mThread;
};
NS_IMPL_ISUPPORTS(IdentityCryptoService, nsIIdentityCryptoService)
@ -225,9 +237,8 @@ IdentityCryptoService::GenerateKeyPair(
return NS_ERROR_UNEXPECTED;
}
nsCOMPtr<nsIRunnable> r = new KeyGenRunnable(keyType, callback);
nsCOMPtr<nsIThread> thread;
nsresult rv = NS_NewThread(getter_AddRefs(thread), r);
nsCOMPtr<nsIRunnable> r = new KeyGenRunnable(keyType, callback, mThread);
nsresult rv = mThread->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
@ -240,9 +251,11 @@ IdentityCryptoService::Base64UrlEncode(const nsACString & utf8Input,
return Base64UrlEncodeImpl(utf8Input, result);
}
KeyPair::KeyPair(SECKEYPrivateKey * privateKey, SECKEYPublicKey * publicKey)
KeyPair::KeyPair(SECKEYPrivateKey * privateKey, SECKEYPublicKey * publicKey,
nsIEventTarget* operationThread)
: mPrivateKey(privateKey)
, mPublicKey(publicKey)
, mThread(operationThread)
{
MOZ_ASSERT(!NS_IsMainThread());
}
@ -328,16 +341,16 @@ KeyPair::Sign(const nsACString & textToSign,
nsCOMPtr<nsIRunnable> r = new SignRunnable(textToSign, mPrivateKey,
callback);
nsCOMPtr<nsIThread> thread;
nsresult rv = NS_NewThread(getter_AddRefs(thread), r);
return rv;
return mThread->Dispatch(r, NS_DISPATCH_NORMAL);
}
KeyGenRunnable::KeyGenRunnable(KeyType keyType,
nsIIdentityKeyGenCallback * callback)
nsIIdentityKeyGenCallback * callback,
nsIEventTarget* operationThread)
: mKeyType(keyType)
, mCallback(new nsMainThreadPtrHolder<nsIIdentityKeyGenCallback>(callback))
, mRv(NS_ERROR_NOT_INITIALIZED)
, mThread(operationThread)
{
}
@ -476,7 +489,7 @@ KeyGenRunnable::Run()
MOZ_ASSERT(privk);
MOZ_ASSERT(pubk);
// mKeyPair will take over ownership of privk and pubk
mKeyPair = new KeyPair(privk, pubk);
mKeyPair = new KeyPair(privk, pubk, mThread);
}
}
}