bug 1620390 - use the background thread pool instead of a one-off thread in OSKeyStore r=bbeurdouche

OSKeyStore doesn't need its own thread and can use the background thread pool instead.

Differential Revision: https://phabricator.services.mozilla.com/D66692

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Dana Keeler 2020-03-17 23:07:48 +00:00
Родитель 35bf0da2a1
Коммит fb868a6c13
2 изменённых файлов: 20 добавлений и 73 удалений

Просмотреть файл

@ -8,6 +8,7 @@
#include "mozilla/Base64.h"
#include "mozilla/dom/Promise.h"
#include "nsThreadUtils.h"
#include "nsXPCOM.h"
#include "pk11pub.h"
@ -22,13 +23,12 @@
# include "NSSKeyStore.h"
#endif
NS_IMPL_ISUPPORTS(OSKeyStore, nsIOSKeyStore, nsIObserver)
NS_IMPL_ISUPPORTS(OSKeyStore, nsIOSKeyStore)
using namespace mozilla;
using dom::Promise;
OSKeyStore::OSKeyStore()
: mKs(nullptr), mKsThread(nullptr), mKsIsNSSKeyStore(false) {
OSKeyStore::OSKeyStore() : mKs(nullptr), mKsIsNSSKeyStore(false) {
MOZ_ASSERT(NS_IsMainThread());
if (NS_WARN_IF(!NS_IsMainThread())) {
return;
@ -49,43 +49,6 @@ OSKeyStore::OSKeyStore()
mKs.reset(new NSSKeyStore());
mKsIsNSSKeyStore = true;
#endif
nsCOMPtr<nsIThread> thread;
nsresult rv = NS_NewNamedThread("OSKeyStore", getter_AddRefs(thread));
if (NS_WARN_IF(NS_FAILED(rv))) {
mKs = nullptr;
return;
}
mKsThread = thread;
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (NS_WARN_IF(!obs)) {
mKsThread = nullptr;
mKs = nullptr;
return;
}
rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
mKsThread = nullptr;
mKs = nullptr;
}
}
NS_IMETHODIMP
OSKeyStore::Observe(nsISupports*, const char* aTopic, const char16_t*) {
MOZ_ASSERT(!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID));
MOZ_ASSERT(NS_IsMainThread());
if (!NS_IsMainThread()) {
return NS_ERROR_NOT_SAME_THREAD;
}
if (mKsThread) {
mKsThread->Shutdown();
mKsThread = nullptr;
mKs = nullptr;
}
return NS_OK;
}
static nsresult GenerateRandom(std::vector<uint8_t>& r) {
@ -282,7 +245,6 @@ OSKeyStore::AsyncUnlock(JSContext* aCx, Promise** promiseOut) {
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -297,7 +259,8 @@ OSKeyStore::AsyncUnlock(JSContext* aCx, Promise** promiseOut) {
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
void BackgroundLock(RefPtr<Promise>& aPromise, RefPtr<OSKeyStore> self) {
@ -321,7 +284,6 @@ OSKeyStore::AsyncLock(JSContext* aCx, Promise** promiseOut) {
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -336,7 +298,8 @@ OSKeyStore::AsyncLock(JSContext* aCx, Promise** promiseOut) {
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
void BackgroundGenerateSecret(const nsACString& aLabel,
@ -369,7 +332,6 @@ OSKeyStore::AsyncGenerateSecret(const nsACString& aLabel, JSContext* aCx,
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -385,7 +347,8 @@ OSKeyStore::AsyncGenerateSecret(const nsACString& aLabel, JSContext* aCx,
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
void BackgroundSecretAvailable(const nsACString& aLabel,
@ -414,7 +377,6 @@ OSKeyStore::AsyncSecretAvailable(const nsACString& aLabel, JSContext* aCx,
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -430,7 +392,8 @@ OSKeyStore::AsyncSecretAvailable(const nsACString& aLabel, JSContext* aCx,
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
void BackgroundRecoverSecret(const nsACString& aLabel,
@ -460,7 +423,6 @@ OSKeyStore::AsyncRecoverSecret(const nsACString& aLabel,
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -477,7 +439,8 @@ OSKeyStore::AsyncRecoverSecret(const nsACString& aLabel,
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
void BackgroundDeleteSecret(const nsACString& aLabel, RefPtr<Promise>& aPromise,
@ -504,7 +467,6 @@ OSKeyStore::AsyncDeleteSecret(const nsACString& aLabel, JSContext* aCx,
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -520,7 +482,8 @@ OSKeyStore::AsyncDeleteSecret(const nsACString& aLabel, JSContext* aCx,
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
static void BackgroundEncryptBytes(const nsACString& aLabel,
@ -554,7 +517,6 @@ OSKeyStore::AsyncEncryptBytes(const nsACString& aLabel,
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -573,7 +535,8 @@ OSKeyStore::AsyncEncryptBytes(const nsACString& aLabel,
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
void BackgroundDecryptBytes(const nsACString& aLabel,
@ -613,7 +576,6 @@ OSKeyStore::AsyncDecryptBytes(const nsACString& aLabel,
}
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_STATE(mKsThread);
RefPtr<Promise> promiseHandle;
nsresult rv = GetPromise(aCx, promiseHandle);
@ -632,7 +594,8 @@ OSKeyStore::AsyncDecryptBytes(const nsACString& aLabel,
}));
promiseHandle.forget(promiseOut);
return mKsThread->Dispatch(runnable.forget());
return NS_DispatchBackgroundTask(runnable.forget(),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
// Generic AES-GCM cipher wrapper for NSS functions.

Просмотреть файл

@ -11,9 +11,7 @@
#define OSKeyStore_h
#include "nsCOMPtr.h"
#include "nsIObserver.h"
#include "nsIOSKeyStore.h"
#include "nsIThread.h"
#include "nsString.h"
#include "ScopedNSSTypes.h"
@ -74,10 +72,9 @@ class AbstractOSKeyStore {
nsresult GetPromise(JSContext* aCx,
/* out */ RefPtr<mozilla::dom::Promise>& aPromise);
class OSKeyStore final : public nsIOSKeyStore, public nsIObserver {
class OSKeyStore final : public nsIOSKeyStore {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIOBSERVER
NS_DECL_NSIOSKEYSTORE
OSKeyStore();
@ -101,20 +98,7 @@ class OSKeyStore final : public nsIOSKeyStore, public nsIObserver {
private:
~OSKeyStore() = default;
// Thread safety for OSKeyStore:
// * mKsThread is only accessed on the main thread
// * mKsThread is shut down on the main thread when OSKeyStore receives the
// "xpcom-shutdown" notification
// * mKs is created and destroyed on the main thread, but is only accessed on
// mKsThread
// * XPCOM notifies "xpcom-shutdown" before shutting down the component
// manager, so mKsThread is shut down before mKs is destroyed
// Essentially, the lifetime of mKsThread is contained by the lifetime of mKs.
// Since the value of mKs never changes while mKsThread is alive and any uses
// of it are serial, there shouldn't be any memory safety issues.
std::unique_ptr<AbstractOSKeyStore> mKs;
nsCOMPtr<nsIThread> mKsThread;
bool mKsIsNSSKeyStore;
const nsCString mLabelPrefix =
NS_LITERAL_CSTRING("org.mozilla.nss.keystore.");