Bug 1583646 - make WebCryptoTask dispatch tasks to our background thread; r=keeler,jcj

Since background threads get shut down near `xpcom-shutdown-threads`,
there's no need to have `WebCryptoThreadPool` anymore; we can rely on
the background thread dispatching to fail to dispatch our task as
appropriate.

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Nathan Froyd 2019-09-26 20:47:31 +00:00
Родитель 1cea1cebca
Коммит c272af49a0
5 изменённых файлов: 7 добавлений и 182 удалений

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

@ -19,7 +19,6 @@
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/WebCryptoCommon.h"
#include "mozilla/dom/WebCryptoTask.h"
#include "mozilla/dom/WebCryptoThreadPool.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/WorkerPrivate.h"
@ -339,7 +338,13 @@ void WebCryptoTask::DispatchWithPromise(Promise* aResultPromise) {
MAYBE_EARLY_FAIL(mEarlyRv);
// dispatch to thread pool
mEarlyRv = WebCryptoThreadPool::Dispatch(this);
if (!EnsureNSSInitializedChromeOrContent()) {
mEarlyRv = NS_ERROR_FAILURE;
}
MAYBE_EARLY_FAIL(mEarlyRv);
mEarlyRv = NS_DispatchToBackgroundThread(this);
MAYBE_EARLY_FAIL(mEarlyRv)
}

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

@ -1,125 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/dom/WebCryptoThreadPool.h"
#include "MainThreadUtils.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsNSSComponent.h"
#include "nsXPCOMCIDInternal.h"
#include "nsXPCOMPrivate.h"
#include "nsIObserverService.h"
#include "nsThreadPool.h"
namespace mozilla {
namespace dom {
StaticRefPtr<WebCryptoThreadPool> gInstance;
NS_IMPL_ISUPPORTS(WebCryptoThreadPool, nsIObserver)
/* static */
void WebCryptoThreadPool::Initialize() {
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
MOZ_ASSERT(!gInstance, "More than one instance!");
gInstance = new WebCryptoThreadPool();
NS_WARNING_ASSERTION(gInstance, "Failed create thread pool!");
if (gInstance && NS_FAILED(gInstance->Init())) {
NS_WARNING("Failed to initialize thread pool!");
gInstance = nullptr;
}
}
/* static */
nsresult WebCryptoThreadPool::Dispatch(nsIRunnable* aRunnable) {
if (gInstance) {
return gInstance->DispatchInternal(aRunnable);
}
// Fail if called on shutdown.
return NS_ERROR_FAILURE;
}
nsresult WebCryptoThreadPool::Init() {
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE);
// Need this observer to know when to shut down the thread pool.
return obs->AddObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, false);
}
nsresult WebCryptoThreadPool::DispatchInternal(nsIRunnable* aRunnable) {
MutexAutoLock lock(mMutex);
if (mShutdown) {
return NS_ERROR_FAILURE;
}
if (!mPool) {
NS_ENSURE_TRUE(EnsureNSSInitializedChromeOrContent(), NS_ERROR_FAILURE);
nsCOMPtr<nsIThreadPool> pool(new nsThreadPool());
nsresult rv = pool->SetName(NS_LITERAL_CSTRING("SubtleCrypto"));
NS_ENSURE_SUCCESS(rv, rv);
pool.swap(mPool);
}
return mPool->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
}
void WebCryptoThreadPool::Shutdown() {
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
// Limit the scope of locking to avoid deadlocking if DispatchInternal ends
// up getting called during shutdown event processing.
nsCOMPtr<nsIThreadPool> pool;
{
MutexAutoLock lock(mMutex);
if (mShutdown) {
return;
}
pool = mPool;
mShutdown = true;
}
if (pool) {
pool->Shutdown();
}
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
NS_WARNING_ASSERTION(obs, "Failed to retrieve observer service!");
if (obs) {
if (NS_FAILED(
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID))) {
NS_WARNING("Failed to remove shutdown observer!");
}
}
}
NS_IMETHODIMP
WebCryptoThreadPool::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
if (gInstance) {
gInstance->Shutdown();
gInstance = nullptr;
}
return NS_OK;
}
} // namespace dom
} // namespace mozilla

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

@ -1,50 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_WebCryptoThreadPool_h
#define mozilla_dom_WebCryptoThreadPool_h
#include "mozilla/Mutex.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIThreadPool.h"
namespace mozilla {
namespace dom {
class WebCryptoThreadPool final : nsIObserver {
public:
NS_DECL_THREADSAFE_ISUPPORTS
static void Initialize();
static nsresult Dispatch(nsIRunnable* aRunnable);
private:
WebCryptoThreadPool()
: mMutex("WebCryptoThreadPool::mMutex"),
mPool(nullptr),
mShutdown(false) {}
virtual ~WebCryptoThreadPool() {}
nsresult Init();
nsresult DispatchInternal(nsIRunnable* aRunnable);
void Shutdown();
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) override;
mozilla::Mutex mMutex;
nsCOMPtr<nsIThreadPool> mPool;
bool mShutdown;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_WebCryptoThreadPool_h

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

@ -13,7 +13,6 @@ EXPORTS.mozilla.dom += [
'KeyAlgorithmProxy.h',
'WebCryptoCommon.h',
'WebCryptoTask.h',
'WebCryptoThreadPool.h'
]
UNIFIED_SOURCES += [
@ -21,7 +20,6 @@ UNIFIED_SOURCES += [
'CryptoKey.cpp',
'KeyAlgorithmProxy.cpp',
'WebCryptoTask.cpp',
'WebCryptoThreadPool.cpp',
]
include('/ipc/chromium/chromium-config.mozbuild')

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

@ -70,7 +70,6 @@
#include "AudioChannelService.h"
#include "mozilla/dom/PromiseDebugging.h"
#include "mozilla/dom/WebCryptoThreadPool.h"
#ifdef MOZ_XUL
# include "nsXULPopupManager.h"
@ -278,8 +277,6 @@ nsresult nsLayoutStatics::Initialize() {
PromiseDebugging::Init();
mozilla::dom::WebCryptoThreadPool::Initialize();
if (XRE_IsParentProcess() || XRE_IsContentProcess()) {
InitializeServo();
}