Bug 1001691 - Implement WebCrypto thread pool r=bz

This commit is contained in:
Tim Taubert 2015-09-11 16:01:20 +02:00
parent 23e9bd9f77
commit c3ced0a78e
4 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1,117 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsXPCOMCIDInternal.h"
#include "nsXPCOMPrivate.h"
#include "nsIObserverService.h"
#include "nsIThreadPool.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_WARN_IF_FALSE(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 (!mPool) {
nsCOMPtr<nsIThreadPool> pool(do_CreateInstance(NS_THREADPOOL_CONTRACTID));
NS_ENSURE_TRUE(pool, NS_ERROR_FAILURE);
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!");
MutexAutoLock lock(mMutex);
if (mPool) {
mPool->Shutdown();
}
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
NS_WARN_IF_FALSE(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

View File

@ -0,0 +1,54 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "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)
{ }
virtual ~WebCryptoThreadPool()
{ }
nsresult
Init();
nsresult
DispatchInternal(nsIRunnable* aRunnable);
void
Shutdown();
NS_IMETHODIMP Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData) override;
mozilla::Mutex mMutex;
nsCOMPtr<nsIThreadPool> mPool;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_WebCryptoThreadPool_h

View File

@ -10,6 +10,7 @@ EXPORTS.mozilla.dom += [
'KeyAlgorithmProxy.h',
'WebCryptoCommon.h',
'WebCryptoTask.h',
'WebCryptoThreadPool.h'
]
UNIFIED_SOURCES += [
@ -17,6 +18,7 @@ UNIFIED_SOURCES += [
'CryptoKey.cpp',
'KeyAlgorithmProxy.cpp',
'WebCryptoTask.cpp',
'WebCryptoThreadPool.cpp',
]
include('/ipc/chromium/chromium-config.mozbuild')
@ -26,6 +28,7 @@ FINAL_LIBRARY = 'xul'
LOCAL_INCLUDES += [
'/security/manager/ssl',
'/security/pkix/include',
'/xpcom/build',
]
MOCHITEST_MANIFESTS += ['test/mochitest.ini']

View File

@ -73,6 +73,7 @@
#include "AudioChannelService.h"
#include "mozilla/dom/DataStoreService.h"
#include "mozilla/dom/PromiseDebugging.h"
#include "mozilla/dom/WebCryptoThreadPool.h"
#ifdef MOZ_XUL
#include "nsXULPopupManager.h"
@ -323,6 +324,8 @@ nsLayoutStatics::Initialize()
mozilla::dom::devicestorage::DeviceStorageStatics::Initialize();
mozilla::dom::WebCryptoThreadPool::Initialize();
return NS_OK;
}