2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-10-27 20:00:05 +03:00
|
|
|
/* 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 "WorkerDebuggerManager.h"
|
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
#include "nsSimpleEnumerator.h"
|
2014-10-27 20:00:05 +03:00
|
|
|
|
2016-02-10 12:45:02 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-10-30 02:02:40 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2016-02-10 12:45:02 +03:00
|
|
|
|
2018-01-30 12:12:26 +03:00
|
|
|
#include "WorkerDebugger.h"
|
2014-10-27 20:00:05 +03:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
2018-01-31 10:21:06 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-02-10 12:45:02 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class RegisterDebuggerMainThreadRunnable final : public mozilla::Runnable
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
2016-01-07 15:35:31 +03:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
bool mNotifyListeners;
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
public:
|
2016-02-10 12:45:02 +03:00
|
|
|
RegisterDebuggerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
|
2016-01-07 15:35:31 +03:00
|
|
|
bool aNotifyListeners)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("RegisterDebuggerMainThreadRunnable")
|
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
|
|
|
, mNotifyListeners(aNotifyListeners)
|
2014-10-27 20:00:05 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
2016-01-07 15:35:31 +03:00
|
|
|
~RegisterDebuggerMainThreadRunnable()
|
2014-10-27 20:00:05 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
2016-02-10 12:45:02 +03:00
|
|
|
WorkerDebuggerManager* manager = WorkerDebuggerManager::Get();
|
|
|
|
MOZ_ASSERT(manager);
|
2016-01-07 15:35:31 +03:00
|
|
|
|
2016-02-10 12:45:02 +03:00
|
|
|
manager->RegisterDebuggerMainThread(mWorkerPrivate, mNotifyListeners);
|
2016-01-07 15:35:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class UnregisterDebuggerMainThreadRunnable final : public mozilla::Runnable
|
2016-01-07 15:35:31 +03:00
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
public:
|
2016-02-10 12:45:02 +03:00
|
|
|
explicit UnregisterDebuggerMainThreadRunnable(WorkerPrivate* aWorkerPrivate)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("UnregisterDebuggerMainThreadRunnable")
|
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
2016-01-07 15:35:31 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~UnregisterDebuggerMainThreadRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
2016-02-10 12:45:02 +03:00
|
|
|
WorkerDebuggerManager* manager = WorkerDebuggerManager::Get();
|
|
|
|
MOZ_ASSERT(manager);
|
2014-10-27 20:00:05 +03:00
|
|
|
|
2016-02-10 12:45:02 +03:00
|
|
|
manager->UnregisterDebuggerMainThread(mWorkerPrivate);
|
2014-10-27 20:00:05 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-30 02:02:40 +03:00
|
|
|
static StaticRefPtr<WorkerDebuggerManager> gWorkerDebuggerManager;
|
2016-02-10 12:45:02 +03:00
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
class WorkerDebuggerEnumerator final : public nsSimpleEnumerator
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
2016-01-07 15:35:31 +03:00
|
|
|
nsTArray<RefPtr<WorkerDebugger>> mDebuggers;
|
2014-10-27 20:00:05 +03:00
|
|
|
uint32_t mIndex;
|
|
|
|
|
|
|
|
public:
|
2016-01-07 15:35:31 +03:00
|
|
|
explicit WorkerDebuggerEnumerator(
|
|
|
|
const nsTArray<RefPtr<WorkerDebugger>>& aDebuggers)
|
|
|
|
: mDebuggers(aDebuggers), mIndex(0)
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID& DefaultInterface() override
|
|
|
|
{
|
|
|
|
return NS_GET_IID(nsIWorkerDebugger);
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
private:
|
2018-08-19 00:22:47 +03:00
|
|
|
~WorkerDebuggerEnumerator() override = default;
|
2014-10-27 20:00:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebuggerEnumerator::HasMoreElements(bool* aResult)
|
|
|
|
{
|
|
|
|
*aResult = mIndex < mDebuggers.Length();
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebuggerEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
if (mIndex == mDebuggers.Length()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
mDebuggers.ElementAt(mIndex++).forget(aResult);
|
2014-10-27 20:00:05 +03:00
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
WorkerDebuggerManager::WorkerDebuggerManager()
|
|
|
|
: mMutex("WorkerDebuggerManager::mMutex")
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebuggerManager::~WorkerDebuggerManager()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2016-02-10 12:45:02 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<WorkerDebuggerManager>
|
|
|
|
WorkerDebuggerManager::GetInstance()
|
|
|
|
{
|
|
|
|
RefPtr<WorkerDebuggerManager> manager = WorkerDebuggerManager::GetOrCreate();
|
|
|
|
return manager.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
WorkerDebuggerManager*
|
|
|
|
WorkerDebuggerManager::GetOrCreate()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!gWorkerDebuggerManager) {
|
|
|
|
// The observer service now owns us until shutdown.
|
|
|
|
gWorkerDebuggerManager = new WorkerDebuggerManager();
|
2017-10-30 02:02:40 +03:00
|
|
|
if (NS_SUCCEEDED(gWorkerDebuggerManager->Init())) {
|
|
|
|
ClearOnShutdown(&gWorkerDebuggerManager);
|
|
|
|
} else {
|
2016-02-10 12:45:02 +03:00
|
|
|
NS_WARNING("Failed to initialize worker debugger manager!");
|
|
|
|
gWorkerDebuggerManager = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gWorkerDebuggerManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebuggerManager*
|
|
|
|
WorkerDebuggerManager::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gWorkerDebuggerManager);
|
|
|
|
return gWorkerDebuggerManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WorkerDebuggerManager, nsIObserver, nsIWorkerDebuggerManager);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebuggerManager::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown observer topic!");
|
2016-02-10 12:45:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebuggerManager::GetWorkerDebuggerEnumerator(
|
|
|
|
nsISimpleEnumerator** aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerDebuggerEnumerator> enumerator =
|
2014-10-27 20:00:05 +03:00
|
|
|
new WorkerDebuggerEnumerator(mDebuggers);
|
|
|
|
enumerator.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebuggerManager::AddListener(nsIWorkerDebuggerManagerListener* aListener)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mListeners.Contains(aListener)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.AppendElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebuggerManager::RemoveListener(
|
|
|
|
nsIWorkerDebuggerManagerListener* aListener)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mListeners.Contains(aListener)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.RemoveElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-02-10 12:45:02 +03:00
|
|
|
nsresult
|
|
|
|
WorkerDebuggerManager::Init()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
void
|
2016-02-10 12:45:02 +03:00
|
|
|
WorkerDebuggerManager::Shutdown()
|
2015-03-17 13:15:19 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
mListeners.Clear();
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
void
|
2016-01-07 15:35:31 +03:00
|
|
|
WorkerDebuggerManager::RegisterDebugger(WorkerPrivate* aWorkerPrivate)
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
2016-01-07 15:35:31 +03:00
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2016-01-07 15:35:31 +03:00
|
|
|
// When the parent thread is the main thread, it will always block until all
|
|
|
|
// register liseners have been called, since it cannot continue until the
|
|
|
|
// call to RegisterDebuggerMainThread returns.
|
|
|
|
//
|
|
|
|
// In this case, it is always safe to notify all listeners on the main
|
|
|
|
// thread, even if there were no listeners at the time this method was
|
|
|
|
// called, so we can always pass true for the value of aNotifyListeners.
|
|
|
|
// This avoids having to lock mMutex to check whether mListeners is empty.
|
|
|
|
RegisterDebuggerMainThread(aWorkerPrivate, true);
|
2014-10-27 20:00:05 +03:00
|
|
|
} else {
|
2016-01-07 15:35:31 +03:00
|
|
|
// We guarantee that if any register listeners are called, the worker does
|
|
|
|
// not start running until all register listeners have been called. To
|
|
|
|
// guarantee this, the parent thread should block until all register
|
|
|
|
// listeners have been called.
|
|
|
|
//
|
|
|
|
// However, to avoid overhead when the debugger is not being used, the
|
|
|
|
// parent thread will only block if there were any listeners at the time
|
|
|
|
// this method was called. As a result, we should not notify any listeners
|
|
|
|
// on the main thread if there were no listeners at the time this method was
|
|
|
|
// called, because the parent will not be blocking in that case.
|
|
|
|
bool hasListeners = false;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
hasListeners = !mListeners.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-02-10 12:45:02 +03:00
|
|
|
new RegisterDebuggerMainThreadRunnable(aWorkerPrivate, hasListeners);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL));
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
if (hasListeners) {
|
2016-01-07 15:35:31 +03:00
|
|
|
aWorkerPrivate->WaitForIsDebuggerRegistered(true);
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 15:35:31 +03:00
|
|
|
WorkerDebuggerManager::UnregisterDebugger(WorkerPrivate* aWorkerPrivate)
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
2016-01-07 15:35:31 +03:00
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2016-01-07 15:35:31 +03:00
|
|
|
UnregisterDebuggerMainThread(aWorkerPrivate);
|
2014-10-27 20:00:05 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-02-10 12:45:02 +03:00
|
|
|
new UnregisterDebuggerMainThreadRunnable(aWorkerPrivate);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL));
|
2014-10-27 20:00:05 +03:00
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
aWorkerPrivate->WaitForIsDebuggerRegistered(false);
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 15:35:31 +03:00
|
|
|
WorkerDebuggerManager::RegisterDebuggerMainThread(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aNotifyListeners)
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
RefPtr<WorkerDebugger> debugger = new WorkerDebugger(aWorkerPrivate);
|
|
|
|
mDebuggers.AppendElement(debugger);
|
2014-10-27 20:00:05 +03:00
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
aWorkerPrivate->SetDebugger(debugger);
|
2015-12-22 14:46:57 +03:00
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
if (aNotifyListeners) {
|
|
|
|
nsTArray<nsCOMPtr<nsIWorkerDebuggerManagerListener>> listeners;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
listeners = mListeners;
|
|
|
|
}
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
for (size_t index = 0; index < listeners.Length(); ++index) {
|
2016-01-07 15:35:31 +03:00
|
|
|
listeners[index]->OnRegister(debugger);
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
aWorkerPrivate->SetIsDebuggerRegistered(true);
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 15:35:31 +03:00
|
|
|
WorkerDebuggerManager::UnregisterDebuggerMainThread(
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
// There is nothing to do here if the debugger was never succesfully
|
|
|
|
// registered. We need to check this on the main thread because the worker
|
|
|
|
// does not wait for the registration to complete if there were no listeners
|
|
|
|
// installed when it started.
|
|
|
|
if (!aWorkerPrivate->IsDebuggerRegistered()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<WorkerDebugger> debugger = aWorkerPrivate->Debugger();
|
|
|
|
mDebuggers.RemoveElement(debugger);
|
|
|
|
|
|
|
|
aWorkerPrivate->SetDebugger(nullptr);
|
2014-10-27 20:00:05 +03:00
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIWorkerDebuggerManagerListener>> listeners;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
listeners = mListeners;
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < listeners.Length(); ++index) {
|
2016-01-07 15:35:31 +03:00
|
|
|
listeners[index]->OnUnregister(debugger);
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:35:31 +03:00
|
|
|
debugger->Close();
|
|
|
|
aWorkerPrivate->SetIsDebuggerRegistered(false);
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:07:41 +03:00
|
|
|
uint32_t
|
|
|
|
WorkerDebuggerManager::GetDebuggersLength() const
|
|
|
|
{
|
|
|
|
return mDebuggers.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebugger*
|
|
|
|
WorkerDebuggerManager::GetDebuggerAt(uint32_t aIndex) const
|
|
|
|
{
|
|
|
|
return mDebuggers.SafeElementAt(aIndex, nullptr);
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:21:06 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|