dom/crypto/WebCryptoThreadPool.cpp
author Mike Hommey <mh+mozilla@glandium.org>
Fri, 11 Jan 2019 16:01:15 +0000
changeset 453570 daf50f25895db073e44d50fecf2e4f6fe873865d
parent 448947 6f3709b3878117466168c40affa7bca0b60cf75b
child 461887 f41cee9bf14931b453838d1bbcbda528e3b064e8
permissions -rw-r--r--
Bug 1519307 - Add a new project to build useful parts of breakpad independently. r=froydnj With `ac_add_options --enable-project=tools/crashreporter` in a mozconfig, `./mach build` builds minidump_stackwalk, dump_syms and fileid. One caveat is that due to limitation in how the build system works currently, it's cumbersome to keep dump_syms as a host program for Gecko, and to make it a target program for this project. For now, keep it as a host program. We're not going to use it on automation, but it's still convenient to have for quick local builds (I've had to resort to awful hacks downstream). Differential Revision: https://phabricator.services.mozilla.com/D16299

/* -*- 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