DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (fddffdeab170)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/* -*- 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 "WorkerDebuggerManager.h"

#include "nsISimpleEnumerator.h"

#include "WorkerPrivate.h"

USING_WORKERS_NAMESPACE

class RegisterDebuggerRunnable final : public nsRunnable
{
  RefPtr<WorkerDebuggerManager> mManager;
  RefPtr<WorkerDebugger> mDebugger;
  bool mHasListeners;

public:
  RegisterDebuggerRunnable(WorkerDebuggerManager* aManager,
                           WorkerDebugger* aDebugger,
                           bool aHasListeners)
  : mManager(aManager), mDebugger(aDebugger), mHasListeners(aHasListeners)
  { }

private:
  ~RegisterDebuggerRunnable()
  { }

  NS_IMETHOD
  Run() override
  {
    mManager->RegisterDebuggerOnMainThread(mDebugger, mHasListeners);

    return NS_OK;
  }
};

BEGIN_WORKERS_NAMESPACE

class WorkerDebuggerEnumerator final : public nsISimpleEnumerator
{
  nsTArray<nsCOMPtr<nsISupports>> mDebuggers;
  uint32_t mIndex;

public:
  explicit WorkerDebuggerEnumerator(const nsTArray<WorkerDebugger*>& aDebuggers)
  : mIndex(0)
  {
    mDebuggers.AppendElements(aDebuggers);
  }

  NS_DECL_ISUPPORTS
  NS_DECL_NSISIMPLEENUMERATOR

private:
  ~WorkerDebuggerEnumerator() {}
};

NS_IMPL_ISUPPORTS(WorkerDebuggerEnumerator, nsISimpleEnumerator);

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;
  }

  nsCOMPtr<nsISupports> element = mDebuggers.ElementAt(mIndex++);
  element.forget(aResult);
  return NS_OK;
};

WorkerDebuggerManager::WorkerDebuggerManager()
: mMutex("WorkerDebuggerManager::mMutex")
{
  AssertIsOnMainThread();
}

WorkerDebuggerManager::~WorkerDebuggerManager()
{
  AssertIsOnMainThread();
}

NS_IMPL_ISUPPORTS(WorkerDebuggerManager, nsIWorkerDebuggerManager);

NS_IMETHODIMP
WorkerDebuggerManager::GetWorkerDebuggerEnumerator(
                                                  nsISimpleEnumerator** aResult)
{
  AssertIsOnMainThread();

  MutexAutoLock lock(mMutex);

  RefPtr<WorkerDebuggerEnumerator> enumerator =
    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;
}

void
WorkerDebuggerManager::ClearListeners()
{
  AssertIsOnMainThread();

  MutexAutoLock lock(mMutex);

  mListeners.Clear();
}

void
WorkerDebuggerManager::RegisterDebugger(WorkerDebugger* aDebugger)
{
  // May be called on any thread!

  bool hasListeners = false;

  {
    MutexAutoLock lock(mMutex);

    hasListeners = !mListeners.IsEmpty();
  }

  if (NS_IsMainThread()) {
    RegisterDebuggerOnMainThread(aDebugger, hasListeners);
  } else {
    nsCOMPtr<nsIRunnable> runnable =
      new RegisterDebuggerRunnable(this, aDebugger, hasListeners);
    MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
      NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL)));

    if (hasListeners) {
      aDebugger->WaitIsEnabled(true);
    }
  }
}

void
WorkerDebuggerManager::UnregisterDebugger(WorkerDebugger* aDebugger)
{
  // May be called on any thread!

  if (NS_IsMainThread()) {
    UnregisterDebuggerOnMainThread(aDebugger);
  } else {
    nsCOMPtr<nsIRunnable> runnable =
      NS_NewRunnableMethodWithArg<RefPtr<WorkerDebugger>>(this,
        &WorkerDebuggerManager::UnregisterDebuggerOnMainThread, aDebugger);
    MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
      NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL)));

    aDebugger->WaitIsEnabled(false);
  }
}

void
WorkerDebuggerManager::RegisterDebuggerOnMainThread(WorkerDebugger* aDebugger,
                                                    bool aHasListeners)
{
  AssertIsOnMainThread();

  MOZ_ASSERT(!mDebuggers.Contains(aDebugger));
  mDebuggers.AppendElement(aDebugger);

  nsTArray<nsCOMPtr<nsIWorkerDebuggerManagerListener>> listeners;
  {
    MutexAutoLock lock(mMutex);

    listeners.AppendElements(mListeners);
  }

  if (aHasListeners) {
    for (size_t index = 0; index < listeners.Length(); ++index) {
      listeners[index]->OnRegister(aDebugger);
    }
  }

  aDebugger->Enable();
}

void
WorkerDebuggerManager::UnregisterDebuggerOnMainThread(WorkerDebugger* aDebugger)
{
  AssertIsOnMainThread();

  MOZ_ASSERT(mDebuggers.Contains(aDebugger));
  mDebuggers.RemoveElement(aDebugger);

  nsTArray<nsCOMPtr<nsIWorkerDebuggerManagerListener>> listeners;
  {
    MutexAutoLock lock(mMutex);

    listeners.AppendElements(mListeners);
  }

  for (size_t index = 0; index < listeners.Length(); ++index) {
    listeners[index]->OnUnregister(aDebugger);
  }

  aDebugger->Disable();
}

END_WORKERS_NAMESPACE